如何使用 Django 创建特定的 if 条件模板标签?
我的问题是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是创建一个过滤器。
并在模板中:
Easiest way is to create a filter.
and in the template:
也许我可以使用 包含标签 。
创建一个像这样的标签:
templatetags/user_extra.py:
Maybe I could use the inclusion tag.
Create a tag like that :
templatetags/user_extra.py :
当所有其他方法都失败时,您可以使用 {% 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/