1

我试图在django-notifications-hq中隐藏通知计数为 0 时隐藏通知计数 我尝试了以下方法,但它没有定期更新并正确显示数字。

 {% live_notify_badge as nc %}
                      {% if nc > 0|add:0 %}
                      <span class="badge-notifications badge badge-pill badge-danger" style="float:right;margin-bottom:-3px;margin-top: -2px !important; margin-left: 10px !important; font-size: 0.6rem;">
                      {% live_notify_badge %}</span> 
                      {% endif %} 
4

1 回答 1

1

nc不是通知的数量。它会生成一些 HTML,这些 HTML 将调用 Javascript 来获取通知的数量。

您可以通过以下方式获取模板中未读通知的数量:

{{ user.notifications.unread.count }}

因此我们可以检查是否存在未读通知,并使用它来呈现{% live_notify_badge %}

{% if user.notifications.unread.exists %}
    <span class="badge-notifications badge badge-pill badge-danger" style="float:right;margin-bottom:-3px;margin-top: -2px !important; margin-left:10px !important; font-size: 0.6rem;">
        {% live_notify_badge %}
    </span> 
{% endif %}

但是请注意,这将在服务器端呈现,这意味着当用户获取页面并且没有通知时,它不会显示徽章。但是,如果稍后有通知,则不会呈现这些通知。

于 2020-09-04T18:49:09.883 回答