クラスベースのビューを使用してツールに関する情報を表示し、そのビューにタグを追加して、主に inline_formsets の使用方法を独学しようとしています。私が抱えている問題は、子オブジェクトのフォームをテンプレートに挿入する方法です。
問題は、子のフォームセットが表示されているが、テンプレートから表示されている親フォームがないことです。
その結果、親フォームは表示されません -
最終的に、これは「Django で何が間違っているのか?」ということになります。質問。
モデルは非常に単純です。ツールには親オブジェクトを定義するいくつかのフィールドがあり、タグはそれに関連する子です。
models.py
from django.db import models
class Tool(models.Model):
content_id = models.CharField(
primary_key=True,
help_text="Confluence ID of the tool document",
max_length=12
)
tool_name = models.CharField(
max_length=64,
help_text="Short name by which the tool is called",
)
purpose = models.TextField(
help_text="A one sentence summary of the tools reason for use."
)
body = models.TextField(
help_text="The full content of the tool page"
)
last_updated_by = models.CharField(
max_length=64
)
last_updated_at = models.DateTimeField()
def __unicode__(self):
return u"%s content_id( %s )" % (self.tool_name, self.content_id)
class ToolTag(models.Model):
description = models.CharField(
max_length=32,
help_text="A tag describing the category of field. Multiple tags may describe a given tool.",
)
tool = models.ForeignKey(Tool)
def __unicode__(self):
return u"%s describes %s" % (self.description, self.tool)
私は標準のクラスベースのフォームを使用しています:
フォーム.py
from django.http import HttpResponseRedirect
from django.views.generic import CreateView
from django.views.generic import DetailView, UpdateView, ListView
from django.shortcuts import render
from .forms import ToolForm, TagsFormSet
from .models import Tool
TagsFormSet = inlineformset_factory(Tool, ToolTag, can_delete='True')
class ToolUpdateView(UpdateView):
template_name = 'tools/tool_update.html'
model = Tool
form_class = ToolForm
success_url = 'inventory/'
ビュー.py
def call_update_view(request, pk):
form = ToolUpdateView.as_view()(request,pk=pk)
tag_form = TagsFormSet()
return render( request, "tools/tool_update.html",
{
'form': form,
'tag_form': tag_form,
'action': "Create"
}
)
そして、私のテンプレートは次のとおりです。
tool_update.html
{% block content %}
<form action="/update/" method="post">
{% csrf_token %}
<DIV>
Tool Form:
{{ form.as_p }}
</DIV>
<DIV>
Tag Form:
{{ tag_form.as_p }}
</DIV>
<input type="submit" value="Submit" />
</form>
{% endblock %}