views:

1289

answers:

1

I have a list of objects, each with it's own checkbox, where the user can select multiple of these. The list is a result of a query.

How can I mark in the view which checkboxes are already selected? There doesn't seem to be an in operator in the template language.

I want something along the lines of:

<input {% if id in selectedIds %}checked {% endif %}>
A: 

You could use a templatetag like the one in this snippet comments:

http://www.djangosnippets.org/snippets/177/

@register.filter
def in_list(value,arg):
    return value in arg

To be used in templates:

The item is 
{% if item|in_list:list %} 
    in list 
{% else %} 
    not in list
{% endif %}

Not very smart, but it works.

brunobord