【问题标题】:How to hide 0 notification count in django-notifications-hq如何在 django-notifications-hq 中隐藏 0 个通知计数
【发布时间】:2020-09-04 18:42:11
【问题描述】:

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 %} 

【问题讨论】:

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

标签: django django-templates django-notification


【解决方案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 %}

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

【讨论】:

  • 那么如果通知为0,是否需要用户刷新页面才能更新?我理解正确吗?
  • @ashes999:是的。 Django 是一个后端。这意味着您在模板中呈现的是服务器发送回客户端的部分。如果你想改变可以改变 DOM 的部分,那么你使用 JavaScript。所以在这种情况下,你应该看看 JavaScript 如何更新徽章。
猜你喜欢
  • 2021-08-01
  • 2019-02-24
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多