我有一个具有两个字段“ id”和“ name”的组织模型。我打算使用动态模型表单集填充它。到目前为止,我的代码如下。
形式
class OrganizationForm(forms.ModelForm):
class Meta:
model = Organization
fields = ('name',)
OrganizationFormset = modelformset_factory(Organization, form=OrganizationForm, fields=('name', ), extra=1)
意见
class OrganizationCreate(CreateView):
model = Organization
form_class = OrganizationForm
def get_context_data(self, **kwargs):
context = super(OrganizationCreate, self).get_context_data(**kwargs)
context['formset'] = OrganizationFormset()
return context
def post(self, request, *args, **kwargs):
formset = OrganizationFormset(request.POST)
if formset.is_valid():
return self.form_valid(formset)
def form_valid(self, formset):
formset.save()
return HttpResponseRedirect('/')
def form_invalid(self, formset):
return self.render_to_response(self.get_context_data(formset=formset))
class OrganizationUpdate(UpdateView):
model = Organization
form_class = OrganizationForm
template_name_suffix = '_update_form'
def get_context_data(self, **kwargs):
context = super(OrganizationUpdate, self).get_context_data(**kwargs)
context['formset'] = OrganizationFormset()
return context
def post(self, request, *args, **kwargs):
formset = OrganizationFormset(request.POST)
if formset.is_valid():
return self.form_valid(formset)
def form_valid(self, formset):
formset.save()
return HttpResponseRedirect('/')
def form_invalid(self, formset):
return self.render_to_response(self.get_context_data(formset=formset))
模板 创建
<form id="myForm" method="post" action="">
{% csrf_token %}
{% for hidden in form.hidden_fields %} {{ hidden }} {% endfor %}
{{ form }}
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %} {{ hidden }} {% endfor %}
<tr>
<td>
{% if form.instance.pk %}{{ form.DELETE }}{% endif %}
{{ form.field1 }}
</td>
<td>{{ form.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ formset.management_form }}
<input type="submit" value="Create Location">
</form>
更新
<form id="myForm" method="post" action="">
{% csrf_token %}
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %} {{ hidden }} {% endfor %}
<tr>
<td>
{% if form.instance.pk %}{{ form.DELETE }}{% endif %}
{{ form.field1 }}
</td>
<td>{{ form.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ formset.management_form }}
<input type="submit" value="Update organizations">
</form>
但是,创建模板和更新模板都显示先前存在的组织,而这仅应在更新模板中发生。此外,删除更新页面中的现有组织将返回以下错误:
The view aims.views.OrganizationUpdate didn't return an HttpResponse object. It returned None instead.
django通用视图和表单集是我的新手。我做错了什么?
提前致谢。
默认情况下django model formsets
将显示它可以从数据库中找到的所有项目。因此queryset
,在创建参数时,必须重写该参数,以使其知道要使用的项目以及何时使用。例如,您应提供-
Organization.objects.none()
对于这样的创建视图-
class OrganizationCreate(CreateView):
...
def get_context_data(self, **kwargs):
...
context['formset'] = OrganizationFormset(queryset=Organization.objects.none()) # providing none
def post(self, request, *args, **kwargs):
...
def form_valid(self, formset):
...
def form_invalid(self, formset):
...
这将迫使表单集呈现空白表单。同样,在更新视图中进行更改以编辑特定项目-
class OrganizationUpdate(UpdateView):
...
def get_context_data(self, **kwargs):
context = super(OrganizationUpdate, self).get_context_data(**kwargs)
context['formset'] = OrganizationFormset(queryset=Organization.objects.get(pk=< get the pk from url when editing>))
return context
def post(self, request, *args, **kwargs):
...
def form_valid(self, formset):
...
def form_invalid(self, formset):
...
希望这可以解决您的问题。如果您仍然发现问题,请使用网址的详细信息和可能的屏幕截图更新问题。我会看到我能做什么。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句