9👍
✅
The easiest way would be to use an assignment tag..
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#assignment-tags
@register.assignment_tag(takes_context=True)
def unread_messages_count(context):
user = context['request'].user
return len(user.messages_unread.all())
{% unread_messages_count as cnt %}
{% if cnt %}
foo
{% endif %}
2👍
you can use a django custom filter https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
def unread_messages_count(user_id):
# x = unread_count ## you have the user_id
return x
and in the template
{% if request.user.id|unread_messages_count > 0 %}
some code...
{% endif %}
- [Django]-Difficulty installing Django Debug Toolbar
- [Django]-Dealing with legacy django project in new localized projects
Source:stackexchange.com