Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:如何在不知道字段名称的情况下迭代表单字段选择?_Python_Django_Django Forms_Django Templates - Fatal编程技术网

Python Django:如何在不知道字段名称的情况下迭代表单字段选择?

Python Django:如何在不知道字段名称的情况下迭代表单字段选择?,python,django,django-forms,django-templates,Python,Django,Django Forms,Django Templates,我有一个表单,其字段是在其_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) sel

我有一个表单,其字段是在其_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())

因此,我无法预先知道该字段的名称

现在,我如何在不知道字段名称的情况下迭代字段选择? 我想要的是:

{%用于表单%]中的字段
一些文字

{%表示值,文本位于field.NAME.choices%} {{text}} {%endfor%} {%endfor%}
我知道field.NAME只返回实际的字段名,不允许我访问其选项。但我还能怎么做呢


非常感谢您的帮助

检查此项

检查此项

我已将RadioSelect用作表单上的小部件。文档会像我想的那样重复广播选项,是的,但这只是因为它们明确指定了字段名(“披头士”)。自定义模板可能会起作用,但我不想为此用例编辑全局模板。我已经在表单上使用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 %}