如何使用 Django 创建特定的 if 条件模板标签?

发布于 2024-08-07 12:40:41 字数 1751 浏览 3 评论 0原文

我的问题是 if 条件。

我想要类似的东西,但不知道该怎么做。

{% 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 %}

在收藏夹管理器中,我创建了:

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

因为在 Django 模板中没有办法像这样做到这一点,我可以做一个像这样的模板标签:

{% 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 %}

但是怎么做呢? 你有更好的主意吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新雨望断虹 2024-08-14 12:40:41

最简单的方法是创建一个过滤器。

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

并在模板中:

{% if restaurant|is_favourite_of:user %}

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 %}
送你一个梦 2024-08-14 12:40:41

也许我可以使用 包含标签

创建一个像这样的标签:

{% 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)}

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)}
毁虫ゝ 2024-08-14 12:40:41

当所有其他方法都失败时,您可以使用 {% exprwhatever %} 标记来计算值并将其粘贴到可以在模板中使用的变量中。我不会让设计师知道这一点,但有时这是除了倒立之外唯一有效的方法……好吧,你知道的。

请参阅http://www.djangosnippets.org/snippets/9/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文