How to hide 0 notification count in django-notifications-hq

718 views Asked by At

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

There are 1 answers

2
willeM_ Van Onsem On BEST ANSWER

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.