Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <pre><code>&gt;&gt;&gt; class CheckboxForm(forms.Form): ... items = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple) ... &gt;&gt;&gt; choices = (('item-1', 'This is item 1'), ('item-2', 'This is item 2'), ('item-3', 'This is item 3')) &gt;&gt;&gt; form = CheckboxForm(initial={'items':('item-2',)}) &gt;&gt;&gt; form.fields['items'].choices = choices &gt;&gt;&gt; print form['items'] &lt;ul&gt; &lt;li&gt;&lt;label for="id_items_0"&gt;&lt;input type="checkbox" name="items" value="item-1" id="id_items_0" /&gt; This is item 1&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;label for="id_items_1"&gt;&lt;input checked="checked" type="checkbox" name="items" value="item-2" id="id_items_1" /&gt; This is item 2&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;label for="id_items_2"&gt;&lt;input type="checkbox" name="items" value="item-3" id="id_items_2" /&gt; This is item 3&lt;/label&gt;&lt;/li&gt; &lt;/ul&gt; &gt;&gt;&gt; </code></pre> <p>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.</p> <p>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.</p> <pre><code># valid submission &gt;&gt;&gt; form = CheckboxForm({'items':('item-2',)}) &gt;&gt;&gt; form.fields['items'].choices = choices &gt;&gt;&gt; print form.is_valid() True &gt;&gt;&gt; print form.cleaned_data {'items': [u'item-2']} # invalid submission, 'item-4' does not exist in the field choices &gt;&gt;&gt; form = CheckboxForm({'items':('item-4',)}) &gt;&gt;&gt; print form.is_valid() False </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload