Django Notifications App Django.How

Author avatar wrote on 26/05/2022

Install Django Notifications App

We will use this app https://github.com/django-notifications/django-notifications – Connect to preview
They have good documentation about how to install and use this app, just read README file.

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}

3.To our main urls.py add this code:


from django.conf.urls import url
import notifications.urls

urlpatterns = [
    ...
    url('^inbox/notifications/', include(notifications.urls, namespace='notifications')),
    ...
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

4.Run migtation command in our terminal:


python manage.py migrate notifications

Now u can use Notifications App.

Customizations for our project

Note: If you want to test with these CSS classes, you can get the open-source CSS file at agilecss.com

1.Add to _header.html file following code inside nav menu

2.Add to the end of _footer.html file this code

3.Add js files in base.html file after _footer.html including

4.Save the following file to our projects js folder and run “python manage.py collectstatic“ command in our terminal

Fire a notification

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')

This example from contact us page