views:

197

answers:

3

Hello,

My problem is a if condition.

I would like somethings like that but cannot figure out how to do it.

{% if restaurant.is_favorite_of(user) %}
     <img src="{{MEDIA_URL}}images/favorite_on.png" alt="This restaurant is one of your favorite (Click to undo)" />
{% else %}
     <img src="{{MEDIA_URL}}images/favorite_off.png" alt="This restaurant is not one of your favorite (Click to add to your favorite)" />
{% endif %}

In the Favorite manager, I created :

def is_favorite(self, user, content_object):
    """
    This method returns :
       - True if content_object is favorite of user
       - False if not
    >>> user = User.objects.get(username="alice")
    >>> fav_user = User.objects.get(username="bob")
    >>> fav1 = Favorite.create_favorite(user, fav_user)
    >>> Favorite.objects.is_favorite(user, fav_user)
    True
    >>> Favorite.objects.is_favorite(user, user)
    False
    >>> Favorite.objects.all().delete()

    Above if we test if bob is favorite of alice it is true.
    But alice is not favorite of alice.
    """
    ct = ContentType.objects.get_for_model(type(content_object))
    try:
        self.filter(user=user).filter(content_type = ct).get(object_id = content_object.id)
        return True
    except Favorite.DoesNotExist:
        return False

Because in Django templates there is no way of doing it likes this, I could do a templatetag that act like that :

{% is_favorite user resto %}
     <img src="{{MEDIA_URL}}images/favorite_on.png" alt="This restaurant is one of your favorite (Click to undo)" />
{% else %}
     <img src="{{MEDIA_URL}}images/favorite_off.png" alt="This restaurant is not one of your favorite (Click to add to your favorite)" />
{% endif %}

But how to do it ? Do you have a better idea ?

+2  A: 

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
+6  A: 

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 %}
Daniel Roseman
Thatś good, I didn't know that we could use the filter in a if condition.
Natim
A: 

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/

Peter Rowell
I also would prefer not use this "spécial tag".
Natim
I agree completely. As I said in a comment many months ago, Django's out-of-the-box template system is lacking in several respects. The claim is that this keeps designers from doing bad things, but my experience is that anything more complicated than a simple if/else ends up getting done by programmers anyway. That means that trivial expressions like this entail creating a lot of custom tag cruft. Doing one or two is no big deal, after that it starts to get old very quickly.
Peter Rowell