【发布时间】:2020-07-26 01:16:36
【问题描述】:
我有一个表单,其字段是在其 __init__ 方法中创建的:
class Form(forms.Form):
def __init__(self, modelInstance, *args, **kwargs):
super(Form, self).__init__(*args, **kwargs)
# create fields based of modelInstance data (because it might change)
self.fields[modelInstance.name] = models.ChoiceField(choices=SomeChoices, widget=forms.RadioSelect())
因此我无法提前知道如何命名该字段。
现在如何在不知道其名称的情况下迭代字段选择?
我想要什么:
{% for field in form %}
<p>sometext</p>
{% for value, text in field.NAME.choices %}
<input type="radio" name="{{ field.name }}" value="{{value}}" id="id_{{ field.name }}_{{forloop.counter0}}">
<label>{{text}}</label>
</div>
{% endfor %}
{% endfor %}
我知道 field.NAME 只返回实际的字段名称,不让我访问它的选择。但是我还能怎么做呢?
非常感谢任何帮助!
【问题讨论】:
标签: python django django-forms django-templates