【问题标题】:Django checkbox questionDjango复选框问题
【发布时间】:2011-01-31 12:17:37
【问题描述】:

您好,我有一个显示项目列表的模板表单。此模板称为 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>

【问题讨论】:

    标签: python django checkbox views django-templates


    【解决方案1】:

    我对你试图做什么感到有点困惑。但是,如果可能的话,我建议您使用 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' kwarg 有一个用于 'items' 字段的键,它应该是默认检查的元素 ID 的可迭代。您可以看到“item-2”作为“items”字段的“初始”值给出,并且在生成的 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。

    【讨论】:

    • 我正在使用 django 表单。我没有上传我的观点。我也有一个清单。我有一个列出项目的模板。从这个模板我希望能够添加一个项目。现在,当我的意思是添加一个项目时,我并不是指创建一个表单。我的意思是,转到列出所有现有客户项目的页面(我有这个模板)。在此模板中有一个列表,您可以在其中选择要添加的现有项目。我遇到的问题是复选框。出于某种原因,它们都在我的代码中“打勾”。我希望模板项目列表中的项目被勾选,并且为空白。
    猜你喜欢
    • 2019-03-03
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2011-11-02
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多