Model Formset - By Default model formset is rendering one extra field (2 fields in total)

Deadly 提交于 2019-12-04 10:17:52

By default, modelformset_factory creates one extra form. If you don't want any extra forms, set extra=0.

Dirset = modelformset_factory(Direction, form=DirectionForm, extra=0)

The KeyError is because you have not included the form's id field in your template. You should have something like:

{% for form in dir_formset %}
    {{ form.id }}
    {{ form.text }}
    ...
{% endfor %}

Note that you should be passing the formset instance dir_formset when rendering the template, not the class DirFormSet. Your view should be something like

return render(request, "test/test.html", {'dir_formset': dir_formset})     

then the template should be updated to use dir_formset instead of DirFormSet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!