Notifications provide updates about the activity on an application. In this guide, you will learn how to set up simple notifications for your Django application. This is Part 1 of this series.
When you're finished, you'll be able to set up simple notifications for your Django app.
Part 1 - Setting up the backend (this tutorial)
Part 2 - Frontend components with TailwindCSS and AlpineJS
Our final product will look something like this.
To complete this tutorial, you will need:
LEVEL - 💻 💻 Beginner to Intermediate - Knowledge of how Django works, for example how views, URL configuration, and templates work. We will cover high-level concepts but you may need to do further reading on your own.
A Notification or Activity in its simplest form consists of an actor, verb, and object. It describes:
Clone our starter repo here and follow the instructions to set up a new project.
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
We will be using the excellent Django notifications library to do some of the heavy lifting for us.
requirements.txt
...
django-notifications-hq==1.6.0
settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'...other apps',
'notifications',
...
)
urls.py
from django.urls import path
import notifications.urls
urlpatterns = [
...
path('inbox/notifications/', include(notifications.urls, namespace='notifications')),
...
]
Notes
The package will take care of:
The above should be sufficient for most apps. However, if you need to customize the notification model in order to extend the functionality you would create a new model:
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
Notes
'notis'
to handle extensions to the base library. Technically the model could be added to any other app. This is more organized as it keeps your notifications logic separate from your other apps.settings.py
..
NOTIFICATIONS_NOTIFICATION_MODEL = 'apps.notis.Notification'
Notes
Let's go through an example of how we can use this on a real application to store user sign-in and sign-out events and display them to the user.
A user story is an informal, natural language description of features of a software system (Wikipedia). An example user story for this would be, "as a user, I want to be able to see my sign-in activity".
Breaking it down into actionable tasks
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.
Additionally, the Django auth framework already provides prebuilt signals for login and logout actions that our code can listen to.
apps/users/signals.py
from django.contrib.auth import user_logged_in, user_logged_out
from notifications.signals import notify
@receiver(user_logged_in)
def user_signed_in(request, user, **kwargs):
notify.send(user, recipient=user, verb=_("You signed in"))
@receiver(user_logged_in)
def user_signed_out(request, user, **kwargs):
notify.send(user, recipient=user, verb=_("You signed out"))
Notes
user_logged_in
& user_logged_out
signals using the @receiver decorator.notify.send
is a signal provided by the Django-notifications library. The actor recipient is the user.notis/apps.py
class NotisConfig(AppConfig):
name = "apps.notis"
def ready(self):
try:
import apps.notis.signals
except ImportError:
pass
Notes:
ready()
method of your application configuration class. This process happens after the app registration process. See the docs for a more technical explanation.In this tutorial, we learned how to set up the backend to handle notifications for our Django app. In the next part, we will create frontend components and connect them to the backend.
Related Articles
All ArticlesSuccess
Error
Warning
Info