[Django]-How to create a specific if condition templatetag with Django?

11👍

Easiest way is to create a filter.

@register.filter
def is_favourite_of(object, user):
    return Favourite.objects.is_favourite(user, object)

and in the template:

{% if restaurant|is_favourite_of:user %}

2👍

Maybe I could use the inclusion tag.

Create a tag like that :

{% show_favorite_img user restaurant %}

templatetags/user_extra.py :

@register.inclusion_tag('users/favorites.html')
def show_favorite_img(user, restaurant):
    return {'is_favorite': Favorite.objects.is_favorite(user, restaurant)}
👤Natim

0👍

When all else fails you can use the {% expr whatever %} tag to compute a value and stick it in a variable that you can use in your template. I don’t let designers know about it, but sometimes it’s the only thing that works short of standing on your head and … well, you know.

See http://www.djangosnippets.org/snippets/9/

Leave a comment