【发布时间】:2012-02-09 22:26:01
【问题描述】:
我是 Django 新手,我正在尝试创建一个包含下拉列表的表单。这是为了在单击提交时根据用户在这些下拉列表中的选择生成脚本。
问题是以下表单模板正在创建重复的表单元素 id
即使要重复下拉菜单,如何在表单模板中创建唯一 ID。
以下是下拉菜单的代码。
<html>
<table border="1">
<form action="/PrintTestScript/" method="post">
{% csrf_token %}
<tr>
<th>Action</th>
<th>Endpoint</th>
<th>Status</th>
<th>Box Type</th>
</tr>
{% for i in 0123456789|make_list %}
<tr>
<td>
{{form.action}}
</td>
<td>
{{form.series}}
</td>
<td>
{{form.epstatus}}
</td>
<td>
{{form.boxtype}}
</tr>
{% endfor %}
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</form>
</table>
</html>
以下是表单类定义。
class TestForm(ModelForm):
action = forms.ModelChoiceField(queryset=actions.objects.all(), empty_label="")
series = forms.ModelChoiceField(queryset=endpoints.objects.all(), empty_label="")
epstatus = forms.ModelChoiceField(queryset=status.objects.all(), empty_label="")
boxtype = forms.ModelChoiceField(queryset=boxtype.objects.all())
class Meta:
model = endpoints
exclude = ('end_point_alias', 'dial_num', 'ip_address')
这是创建视图的地方
def getvals(request):
form = TestForm()
return render_to_response('main.html', {'form':form}, context_instance=RequestContext(request))
感谢您的帮助。
【问题讨论】:
标签: django django-models django-forms django-templates django-views