2

I ham trying to hide the notification count when it's 0 in django-notifications-hq I have tried the below method but it is not updating regularly and displaying the number correctly.

 {% 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 %} 
ashes999
  • 1,234
  • 1
  • 12
  • 36
  • 1
    `nc` is not the number of notifications. It generates some HTML that will make Javascript calls to fetch the number of notifications. – Willem Van Onsem Sep 04 '20 at 18:45

1 Answers1

2

nc is not the number of notifications. It generates some HTML that will make Javascript calls to fetch the number of notifications.

You can obtain the number of unread notifications in the template with:

{{ user.notifications.unread.count }}

So we can check if an unread notification exists, and use this to render the {% 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 %}

Note however that this will be rendered at server side, so that means that when the user fetches the page, and there are no notifications, it will not display the badge. If however later there are notifictions these will not be rendered.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • so if the notification is 0, the user is required to refresh the page for it to update? Have I understood correctly? – ashes999 Sep 04 '20 at 19:18
  • 1
    @ashes999: yes. Django is a *backend*. So that means that what you render in the templates is the part the server sends back to the client. If you want to alter the part that can change the DOM, then you use JavaScript. So in that case, you should look how JavaScript updates the badge. – Willem Van Onsem Sep 04 '20 at 19:44