django模板标记中formset和formset.forms之间的差异
我正在尝试在模板的表单集中循环表单。我已经看到了两种不同的方法,我使用哪一种似乎对我的代码没有影响django模板标记中formset和formset.forms之间的差异,django,django-forms,django-templates,templatetags,Django,Django Forms,Django Templates,Templatetags,我正在尝试在模板的表单集中循环表单。我已经看到了两种不同的方法,我使用哪一种似乎对我的代码没有影响 {{ formset.management_form }} {% for form in formset %} {{ form }} {% endfor %} 而且 {{ formset.management_form }} {% for form in formset.forms %} {{ form }} {%
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
而且
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form }}
{% endfor %}
这有什么区别吗?为什么要将.forms放在末尾?根据类的来源: 这两种方法(
用于表单集中的表单
和用于表单集中的表单。表单
)是相同的
您可以看到,每次self.forms
时,用于for
循环的iter
都会产生。另一方面,formset.forms中form的迭代相同的东西,self.forms
def __iter__(self):
"""Yields the forms in the order they should be rendered"""
return iter(self.forms)
@cached_property
def forms(self):
"""
Instantiate forms at first property access.
"""
# DoS protection is included in total_form_count()
forms = [self._construct_form(i, **self.get_form_kwargs(i))
for i in range(self.total_form_count())]
return forms