简体   繁体   English

如何获取django模板中的表单字段数?

[英]How to get the number of form fields in a django template?

I have a django form that I can iterate over, for example, through a for loop: 我有一个django表单,我可以迭代,例如,通过for循环:

{% for field in form %}
   ...
{% endfor %}

Now I'm trying to find out the total number form fields outside of the for loop. 现在我试图找出for循环之外的总数表单字段。 I have tried the following, but it just returns 0 , even though I have 2 form fields: 我尝试了以下,但它只返回0 ,即使我有2个表单字段:

{{ form|length }}

Is there any way to do this? 有没有办法做到这一点?

PS: This is in the context of django-cms 3.1.3, if this helps. PS:这是在django-cms 3.1.3的背景下,如果这有帮助的话。

You'll need to explicitly count the fields, rather than just the form itself, such as: 您需要显式计算字段,而不仅仅是表单本身,例如:

{{ form.fields|length }}

As seen in the Django form documentation Django表单文档中所示

试试这个:{{form.hidden_​​fields | length + form.visible_fields | length}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM