开发者

Django checkbox question

开发者 https://www.devze.com 2023-02-08 04:05 出处:网络
Hello I have a template form which displays a list of items. This template is called edit_order.html. I want to be able to add a new item from another list of items. From the other list of items is a

Hello I have a template form which displays a list of items. This template is called edit_order.html. I want to be able to add a new item from another list of items. From the other list of items is a template called items.html, showing a list of items. In items.html, each item has a checkbox besides an item. Now, what I want to do is make a checkbox marked only if the item is already listed in the edit_order template. Right now, all of the items are marked. but I don't want this.

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.typ开发者_JS百科e}}</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>


I'm a bit confused as to what you are attempting to do. However, I would suggest, if possible, that you make use of the forms library included with Django instead of rendering a bunch of form elements manually in the template. Here is an example of a simple form with custom/dynamic choices rendered as checkboxes.

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

Notice that 'initial' kwarg given to the form constructor has a key for the 'items' field, which should be an iterable of IDs of the elements to be checked by default. You can see that 'item-2' is given as the 'initial' value for the 'items' field, and in the resulting HTML display, 'item-2' is checked. So by customizing this 'initial' argument you can specify which of the items are checked initially on your page.

If you use Django forms, you can also validate the submitted form data easily. You don't need to give the form 'initial' when binding it to input data, since it doesn't matter what items were/are initially selected.

# 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

Note: You could also setup a custom constructor on the form and pass the choices into that instead of setting the field.choices after the form is created.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号