On 18 avr, 18:27, Shawn Milochik <[email protected]> wrote:
> You can do this pretty easily in Python with any():
>
> if any(['foo' in x for x in my_list]):
>     #do something

should be:

if any(x['id'] == 'foo' for x in mylist):
    do_something()


Your above snippet will test if a dict in my_list has a *key* named
'foo'.

> However, I don't know of a way to do it in a template, which is what
> it appears you're asking.

# myapp/templatetags/mytags.py
@register.filter
def test_id(list_of_dicts, val):
    for dic in list_of_dicts:
        if dic.get('id', '') == val:
            return True
    return False


# mytemplate.html

{% load mytags %}
{% if my_list|test_id:"foo"%}
   <b>yeps, it's here</b>
{% endif %}

Not tested but you get the idea.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to