カスタムユーザーの多対多のフィールド長の1つをカウントするdjangoテンプレートタグを作成しました。
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def unread_messages_count(context):
user = context['request'].user
return len(user.messages_unread.all())
テンプレート自体の中で、ゼロより大きい場合にのみユーザーに表示したいので、次のことを試しました。
{% ifnotequal unread_messages_count 0 %}
some code...
{% endifnotequal %}
しかし、明らかにそれは機能しませんでした。'with'ステートメントでもありません:
{% with unread_messages_count as unread_count %}
{% ifnotequal unread_count 0 %}
some code...
{% endifnotequal %}
{% endwith %}
変数が0より大きいことを確認し、大きい場合にのみ、ユーザーにコード(変数自体の数値を含む)を提示するにはどうすればよいですか。ありがとう。