无法在 Django 中的模板上呈现 ChoiceField
Unable to render ChoiceField on template in Django
我试图在 Django 的模板中显示 ChoiceField,但我无法让它工作。
我在这里找到了一些解决方案,但似乎对我不起作用 (Possible solution),但我收到错误:too many values to unpack
在线 {{ form.as_p }}
。
所以在网上搜索时,我找到了这个 Solution 但我无法添加到我的代码中并使其正常工作。我得到一个 TextField 而不是 "Dropdown"(在 Django Choicefield 中)。而且,此解决方案列出了 for 循环中的所有项目,我得到 4 个文本字段,而不是 2 个带有元素的选择字段。
我的 forms.py
看起来像:
class SimpleDeploy(forms.Form):
def __init__(self, networkList, policiesList, *args, **kwargs):
super(SimpleDeploy, self).__init__(*args, **kwargs)
if networkList and policiesList:
self.fields['networkPartitions'] = forms.ChoiceField(choices=networkList)
self.fields['applicationPolicies'] = forms.ChoiceField(choices=policiesList)
else:
self.fields['networkPartitions'] = forms.ChoiceField(choices='No network partitions found')
self.fields['applicationPolicies'] = forms.ChoiceField(choices='No application policies found')
在我的 views.py
上:
def simpleDeploy(request):
netList = getDetailsNetworkPartitions(request)
polList = getDetailsApplicationPolicies(request)
if request.method == 'POST':
abs(5) #Nothing here by the moment
else:
simpleForm = SimpleDeploy(networkList=netList, policiesList=polList)
return render(request, 'apacheStratos/simpleDeploy.html', {'form': simpleForm})
其中 netList
和 polList
是元组列表,例如:
[(u'application-policy-2', u'application-policy-2'), (u'application-policy-1', u'application-policy-1')]
在我的模板上,我试图显示 ChoiceField,如:
<table class="table">
{% for item in form.networkPartitions.field.choices %}
<label for="">Network Partitions</label> <input type="choicefield" name="networkPartitions" value="{{item.1}}"/>
{% endfor %}
{% for item in form.applicationPolicies.field.choices %}
<label for="">Application Policies</label> <input type="choicefield" name="applicationPolicies" value="{{item.1}}"/>
{% endfor %}
</table>
如何在不使用 for 循环的情况下获取选择字段并访问元素?我做错了什么?
谢谢。
感谢@raphv,将解决方案放在模板上 {{form.networkPartitions}}
和 {{form.applicationPolicies}}
。就这么简单....
我试图在 Django 的模板中显示 ChoiceField,但我无法让它工作。
我在这里找到了一些解决方案,但似乎对我不起作用 (Possible solution),但我收到错误:too many values to unpack
在线 {{ form.as_p }}
。
所以在网上搜索时,我找到了这个 Solution 但我无法添加到我的代码中并使其正常工作。我得到一个 TextField 而不是 "Dropdown"(在 Django Choicefield 中)。而且,此解决方案列出了 for 循环中的所有项目,我得到 4 个文本字段,而不是 2 个带有元素的选择字段。
我的 forms.py
看起来像:
class SimpleDeploy(forms.Form):
def __init__(self, networkList, policiesList, *args, **kwargs):
super(SimpleDeploy, self).__init__(*args, **kwargs)
if networkList and policiesList:
self.fields['networkPartitions'] = forms.ChoiceField(choices=networkList)
self.fields['applicationPolicies'] = forms.ChoiceField(choices=policiesList)
else:
self.fields['networkPartitions'] = forms.ChoiceField(choices='No network partitions found')
self.fields['applicationPolicies'] = forms.ChoiceField(choices='No application policies found')
在我的 views.py
上:
def simpleDeploy(request):
netList = getDetailsNetworkPartitions(request)
polList = getDetailsApplicationPolicies(request)
if request.method == 'POST':
abs(5) #Nothing here by the moment
else:
simpleForm = SimpleDeploy(networkList=netList, policiesList=polList)
return render(request, 'apacheStratos/simpleDeploy.html', {'form': simpleForm})
其中 netList
和 polList
是元组列表,例如:
[(u'application-policy-2', u'application-policy-2'), (u'application-policy-1', u'application-policy-1')]
在我的模板上,我试图显示 ChoiceField,如:
<table class="table">
{% for item in form.networkPartitions.field.choices %}
<label for="">Network Partitions</label> <input type="choicefield" name="networkPartitions" value="{{item.1}}"/>
{% endfor %}
{% for item in form.applicationPolicies.field.choices %}
<label for="">Application Policies</label> <input type="choicefield" name="applicationPolicies" value="{{item.1}}"/>
{% endfor %}
</table>
如何在不使用 for 循环的情况下获取选择字段并访问元素?我做错了什么?
谢谢。
感谢@raphv,将解决方案放在模板上 {{form.networkPartitions}}
和 {{form.applicationPolicies}}
。就这么简单....