我有选择地呈现表单上的字段。
class SomeForm(forms.Form):
foo = forms.ChoiceField(label='Some Foo', ...)
bar = forms.BooleanField(label='Some Bar', ...)
...
我有一个自定义标签,基于其他一些逻辑,我可以使用 FIELD
迭代我需要的表单字段。标签中的上下文变量:
{% fieldsineed %}
{% if FIELD.field.widget|klass == "CheckboxInput" %}
<li>{{ FIELD }} {{ FIELD.field.label }}</li>
{% else %}
<li>{{ FIELD.label }}: {{ FIELD }}</li>
{% endif %}
{% endfieldsineed %}
( klass
是我从 here 获得的过滤器,它返回过滤值的类名。)
不幸的是,FIELD.label
只是一个字符串。有没有一种简单的方法来渲染 <label>
给定表单字段的标签?
最佳答案
https://docs.djangoproject.com/en/dev/topics/forms/#s-looping-over-the-form-s-fields
显示你可以做到
{{ FIELD.label_tag }}
应该呈现类似的内容
<label for="id_fieldName">Fieldlabel:</label>
关于Django - 渲染单个表单字段的 <label>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6208952/