views:

45

answers:

2

How do I do a if a in list_b test in Django 0.96 Templates?

I'm having a list of checkboxes and a variable containing a list of corresponding values from a previous submit of the checkboxes.

Now I want to set all checkboxes in the list to checked="checked" and I usually use a "if a in listb" test for this. But this doesn't seem to be possible in Django 0.96 templates?

+2  A: 

It's not possible in any version of Django templates. You need to write a custom template tag, or use something like the smart-if tag.

However I suspect you should be doing this in the view, by using Django's forms framework.

Daniel Roseman
Thanks Mate, You might be correct Django's forms framework. How ever at the moment I want to roll my so custom template tag it is (I already have some custom tags setup in my project).(It's really a hugh lack in the template language (considering it being a very common expression in Python), do you know if this is solved in newer versions of the Django Template language?)
Johan Carlsson
A: 

You could also just use jQuery or a similar Javascript library which is more suited for DOM scripting than Django. Also, it should makes your views.py considerably less bloated and thus easier to read and maintain.

Etienne