I’ve following scenario:
- The user loads
home.html, selects a subset of the five checkboxes and clicks on submit. - The user lands on
stats.html, which will show values related to the selected checkboxes AND marks the specific checkboxes as CHECKED.
I’m passing a variable called ‘itemsToCheck’ with following content to the stats.html template:
[(u'8', u'on'), (u'7', u'on')]
Where those values should help me to select specific checkboxes like this:
<input type="checkbox" checked="yes" name="{{ MyModel.id }}"/>
How would you accomplish that (using a template syntax like checked={% something %} or maybe jQuery)?
I’m a Django/web development beginner, so please let me know if my approach is completely wrong and if there is a cleaner/more elegant solution.
This will work if you pass the a dict instead of a list:
For the described list:
you’ll have to do some more processing.
EDIT:
Or in case when dict keys are strings:
Or just prepare the itemsToCheck in a format to skip the conversion…
EDIT 2:
Actually, I’d use a tag here:
and then in checkbox.html: