Django 复选框问题
你好,我有一个模板表单,用来显示一系列的项目。这个模板叫做 edit_order.html。我想从另一个项目列表中添加一个新项目。那个项目列表的模板叫做 items.html,里面显示了一些项目。在 items.html 中,每个项目旁边都有一个复选框。现在,我想做的是:只有当某个项目已经在 edit_order 模板中列出时,那个复选框才会被选中。目前,所有的项目复选框都是选中的,但我并不想要这样。
edit_order.html
{% for item in items %}
<tr>
<td><input type="checkbox" name="item" value="{{item.pk}}" checked="checked"></td>
<td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td>
<td>{{item.type}}</td><td>{{item.format}}</td>
</tr>
{% endfor %}
item.html
{% extends "base_menu.html" %}
{%block script%}
<script type="text/javascript">
$(function(){
$("#check_all").click(function(){
if(this.checked ==true)
$("tbody :checkbox").each(function(){
this.checked=true;
});
else
$("tbody :checkbox").each(function(){
this.checked=false;
});
});
});
</script>
{%endblock%}
<td><a href="{% url tiptop.views.edit_item item.client.pk item.pk %}" onclick="return showAddAnotherPopup(this);">Edit</a></td>
</tr>
{% endfor %}
</tbody>
</table></fieldset>
</div>
<div id="form_footer">
<input type="submit" value="Request Delivery" onclick="change_action('{% url tiptop.views.service_order client.pk 1 %}')">
<input type="submit" value="Request Pick Up" onclick="change_action('{% url tiptop.views.service_order client.pk 2 %}');validate_status(this.form)">
</div>
</form>
{% endblock %}
{% block right_content %}
<div id="location_header">{{client}}: Search results</div>
<form action="{% url tiptop.views.service_order client.pk 1 %}" method="post" onsubmit="return validate_selection(this)">
<div class="form_container">
<fieldset class="model">
<table id="items_table">
<thead>
<tr>
<th><input type="checkbox" id="check_all" checked="checked"></th>
<th scope="col">Tiptop no.</th><th scope="col">Client no.</th><th scope="col">Title</th><th scope="col">Type</th>
<th scope="col">Format</th><th scope="col">Status</th><th scope="col">Date</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr class="items_table_row">
<td><input type="checkbox" name="{{item.pk}}" value="{{item.pk}}" checked="checked"></td>
<td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td><td>{{item.type}}</td><td>{{item.format}}</td>
<td><span id="{{item.pk}}" name="type">{{item.itemstatushistory_set.latest}}</span></td><td>{{item.itemstatushistory_set.latest.date.date|date:"d M Y"}}</td>
1 个回答
3
我有点搞不清楚你想做什么。不过,我建议你如果可以的话,使用Django自带的表单库,而不是在模板里手动渲染一堆表单元素。下面是一个简单的表单示例,它的选项是动态的,并且以复选框的形式展示。
>>> class CheckboxForm(forms.Form):
... items = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
...
>>> choices = (('item-1', 'This is item 1'), ('item-2', 'This is item 2'), ('item-3', 'This is item 3'))
>>> form = CheckboxForm(initial={'items':('item-2',)})
>>> form.fields['items'].choices = choices
>>> print form['items']
<ul>
<li><label for="id_items_0"><input type="checkbox" name="items" value="item-1" id="id_items_0" /> This is item 1</label></li>
<li><label for="id_items_1"><input checked="checked" type="checkbox" name="items" value="item-2" id="id_items_1" /> This is item 2</label></li>
<li><label for="id_items_2"><input type="checkbox" name="items" value="item-3" id="id_items_2" /> This is item 3</label></li>
</ul>
>>>
注意,在表单构造函数中传入的'initial'参数里,有一个'items'字段的键,这个字段应该是一个可迭代的ID列表,用来指定默认选中的元素。你可以看到,'item-2'被设置为'items'字段的初始值,因此在生成的HTML中,'item-2'是被选中的。通过自定义这个'initial'参数,你可以指定在页面上最开始哪些选项是被选中的。
如果你使用Django表单,你也可以很容易地验证提交的表单数据。当你把表单和输入数据绑定时,其实不需要给表单设置'initial',因为最开始选中了哪些选项并不重要。
# valid submission
>>> form = CheckboxForm({'items':('item-2',)})
>>> form.fields['items'].choices = choices
>>> print form.is_valid()
True
>>> print form.cleaned_data
{'items': [u'item-2']}
# invalid submission, 'item-4' does not exist in the field choices
>>> form = CheckboxForm({'items':('item-4',)})
>>> print form.is_valid()
False
注意:你也可以在表单上设置一个自定义构造函数,把选项传进去,而不是在表单创建后再设置字段的选择项。