how do i display notifications from `django-notification`?

  • Last Update :
  • Techknowledgy :

The answer is you have to build it into your own templates. This can be as simple as the following snippet:

<table>
    <caption>{% trans "Notices" %}</caption> 
    <thead>
        <tr>
            <th>{% trans "Type" %}</th>
            <th>{% trans "Message" %}</th>
            <th>{% trans "Date of the Notice" %}</th>
        </tr>
    </thead>
    <tbody>
        {% for notice in notices %}
            {% if notice.is_unseen %}
                <tr class="unseen_notice">
            {% else %}
                <tr class="notice">
            {% endif %}
                <td class="notice_type">[{% trans notice.notice_type.display %}]</td>
                <td class="notice_message">{{ notice.message|safe }}</td>
                <td class="notice_time">{{ notice.added|timesince }} {% trans "ago" %}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

Suggestion : 2

How do I display notifications from `django-notification`?,As @googletorp answered, Pinax is the goto place for figuring out how the authors are using django-notification. In particular, there is a notification administration page that can serve as a handy guide.,Django - How do I display data from a dictionary from views to html file,In my Django template, how do I display the check boxes and notification types?

The answer is you have to build it into your own templates. This can be as simple as the following snippet:

<table>
    <caption>{% trans "Notices" %}</caption> 
    <thead>
        <tr>
            <th>{% trans "Type" %}</th>
            <th>{% trans "Message" %}</th>
            <th>{% trans "Date of the Notice" %}</th>
        </tr>
    </thead>
    <tbody>
        {% for notice in notices %}
            {% if notice.is_unseen %}
                <tr class="unseen_notice">
            {% else %}
                <tr class="notice">
            {% endif %}
                <td class="notice_type">[{% trans notice.notice_type.display %}]</td>
                <td class="notice_message">{{ notice.message|safe }}</td>
                <td class="notice_time">{{ notice.added|timesince }} {% trans "ago" %}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

Suggestion : 3

There are two possible API calls that can be made:,There are some other QuerySet methods, too.,Core contributors (in alphabetical order):, Code review

Make a shallow copy of kwargs and assign it to the data attribute.
It allows overriding the save method of the model which can modify data
if needed.

Installation is easy using pip and will install all required libraries.

$ pip install django - notifications - hq

or get it from source

$ git clone https: //github.com/django-notifications/django-notifications
   $ cd django - notifications
$ python setup.py sdist
$ pip install dist / django - notifications - hq *

The app should go somewhere after all the apps that are going to be generating notifications like django.contrib.auth

INSTALLED_APPS = (
   'django.contrib.auth',
   ...
   'notifications',
   ...
)

Generating notifications is probably best done in a separate signal.

from django.db.models.signals
import post_save
from notifications.signals
import notify
from myapp.models
import MyModel

def my_handler(sender, instance, created, ** kwargs):
   notify.send(instance, verb = 'was saved')

post_save.connect(my_handler, sender = MyModel)

To generate an notification anywhere in your code, simply import the notify signal and send it with your actor, recipient, and verb.

from notifications.signals
import notify

notify.send(user, recipient = user, verb = 'you reached level 10')

api/unread_count/ that returns a javascript object with 1 key: unread_count eg:

{
   "unread_count": 1
}

api/unread_list/ that returns a javascript object with 2 keys: unread_count and unread_list eg:

{
   "unread_count": 1,
   "unread_list": [--list of json representations of notifications--]
}

In the area where you are loading javascript resources add the following tags in the order below:

<script src="{% static 'notifications/notify.js' %}" type="text/javascript"></script>
{% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' %}

To insert a live-updating unread count, use the following template:

{
   % live_notify_badge %
}

Suggestion : 4

Django Notifications App,Now u can use Notifications App.,Important that ‘notifications’ must be after ‘django.contrib.auth’. Also add this string:,You can add a notification in view.py in any method

1.Run command in our terminal:

pip install django - notifications - hq

2.Add to main settings.py:

INSTALLED_APPS = [
   ....
   'django.contrib.auth',
   'notifications',
   ....
]

Important that ‘notifications’ must be after ‘django.contrib.auth’. Also add this string:

DJANGO_NOTIFICATIONS_CONFIG = {
   'USE_JSONFIELD': True
}

4.Run migtation command in our terminal:

python manage.py migrate notifications

You can add a notification in view.py in any method

#Make notification to admin
recipient = User.objects.get(id = 1)
notify.send(recipient, recipient = recipient, verb = 'New Contact us request')

Suggestion : 5

We added business logic to create a notification instance whenever the signal is received. notify.send is a signal provided by the Django-notifications library. The actor recipient is the user.,Behind the scenes, the Django-notifications library will create a notification instance when the signal is dispatched.,Provides a manager with the required business logic to handle typical actions on notifications e.g. filtering, mark as read etc.,We need to find a way to get notified when certain actions occur. Fortunately, we don't have to look too far, Django provides a signal dispatcher for just this purpose. You can read more about it here.

Create a new app called 'notis' in the apps directory.

$ mkdir apps / notispython django - admin.py startapp notis. / apps / notis

Our final folder structure will look like this.

├──
project_name
   |
   └──config...# settings, urls.py etc. |
   └──apps |
   └──notis├── templates |
   ├──..# see part 2
for this└── manage.py

requirements.txt

...
django - notifications - hq == 1.6 .0

urls.py

from django.urls
import path
import notifications.urls

urlpatterns = [
   ...
   path('inbox/notifications/', include(notifications.urls, namespace = 'notifications')),
   ...
]

apps/notis/models.py

from django.db
import models
from notifications.base.models
import AbstractNotification

class Notification(AbstractNotification):
   # custom field example
category = models.ForeignKey('myapp.Category',
   on_delete = models.CASCADE)

class Meta(AbstractNotification.Meta):
   abstract = False