내 문제는 if 조건입니다.Django를 사용하여 특정 조건을 templatetag 생성하는 방법은 무엇입니까?
나는 그와 같은 일을하고 싶지만 어떻게해야 할지를 알 수 없다.
{% 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 %}
는 즐겨 찾기 관리자, 나는 만든 :
{% 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 %}
:
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
장고가이 좋아하는 일을 방법이 없습니다 템플릿에 있기 때문에, 내가처럼 말하고 templatetag을 할 수
하지만 어떻게해야합니까? 더 좋은 아이디어가 있습니까?
http://www.djangosnippets.org/snippets/9/를 참조하십시오. – Natim