views:

25

answers:

1

Hi,

was wondering if there is a way to test if a variable is inside of a list or dict in django using the built in tags and filters.

Ie: {% if var|in:the_list %}

I don't see it in the docs, and will attempt something custom if not, but I don't want to do something that has already been done.

Thanks

+5  A: 

In Django 1.2, you can just do

{% if var in the_list %}

as you would in Python.

Otherwise yes, you will need a custom filter - it's a three-liner though:

@register.filter
def is_in(var, obj):
    return var in obj
Daniel Roseman