2011-01-31 58 views
1

你好,我有一个模板表单,显示项目列表。这个模板被称为edit_order.html。我希望能够从另一个项目列表中添加一个新项目。从另一个项目列表是一个名为items.html的模板,显示项目列表。在items.html中,每个项目除了项目之外还有一个复选框。现在,我想要做的仅仅是当项目已经在edit_order模板中列出的时候,使复选框被标记。现在,所有的项目都被标记。但我不想要这个。Django复选框问题

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> 

回答

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> 
>>> 

注意,“初始” kwarg给予形式构造具有用于“项目”字段的密钥,这应该是元件的ID的一个可迭代到默认进行检查。您可以看到'item-2'作为'items'字段的'initial'值给出,并且在生成的HTML显示中,'item-2'被选中。因此,通过自定义此“初始”参数,您可以指定在页面上最初检查哪些项目。

如果您使用Django表单,您还可以轻松验证提交的表单数据。当将它绑定到输入数据时,您不需要提供表单“初始”,因为最初选择哪些项目并不重要。

# 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 

注意:您还可以设置窗体上的自定义构造函数,并通过选择成,而不是在创建表格后设置field.choices的。

+0

我正在使用django表单。我没有上传我的意见。我也有一个清单。我有一个列出项目的模板。从这个模板我想能够添加一个项目。现在当我的意思是通过添加一个项目,我不是通过创建一个表单。我的意思是,去页面列出所有现有的客户端项目(我有这个模板)。在此模板中有一个列表,您可以选择要添加的现有项目。我遇到问题的是复选框。出于某种原因,我的代码中都是“勾选”的。我希望勾选模板项目列表中的项目,并留空。 – Shehzad009 2011-01-31 15:31:15