How to add social media like notifications to a Django app.

Dasu Gautham Sreeram
Jan 15 · 6 min read

Hi there, in this blog lets see how we can try adding notifications, like that of any social media applications to a django powered app.

So not taking much of your time lets dive into the idea of the app. The idea is to have list of users displayed on the screen and only logged-in users can send a message to other users visible on the screen. Now we try adding notifications feature to this app. Per say when a message is sent from a user to another user then the user must be notified about the same that an message has arrived.

Going into the details of the app, we use django-notifications-hq package for notifications. You can go through different tutorials on how to setup the virtual environment and setting django project without creating any problem with the global python present in the system. I suggest this blog which has series of blogs which gives an hands on experience and a better overview about django.

The final screen of the app is shown as below:

Final screen of the app

We can see that there is a bell icon present in the navbar and also if the user is not authenticated(logged in) then name is changed to guest user. Also the list of all users are shown along with a text box for filling the message and sending the message to the user.

Three dummy users have been created and all the users were given staff permissions so that they can login from the admin page itself of our simple app, and we need not write an authentication app again for the login and logout sessions of the user.

Say we are logged in with the credentials of user1 the screen would look like this:

We can see only the users other than the user logged in are present and the list of unread notifications is seen on top of bell icon and also the name of the user logged in.

Note: We use the django admin site for logging in to the app and then again redirecting to the index site.

A sample demo video of our app can be seen as following:

The idea is simple we have the notification tags enable for in the templates of our django app and there is a {% live_notify_badge %} which gives count of unread notifications present for a user in an app and also it comes inbuilt with the django-notification-hq package and it pings the server for every fixed amount of time(say 5 seconds) and it gets updated on the template rendered. So in our case we have the bell icon on the navigation bar and the live notification badge is embedded into the template and is placed in the circle above the bell button. So when any new notification arrives the live notify badge pings the server and updates the count of the unread notifications.

For our simple app there are only two methods which is an index method which renders list of all the users and the logged in user for sending the message. Another method deals with the sending of notification from one user to another with the message and the type of the message.

from notifications.signals import notify

This is how we send a message or notification from one user to another. So in the view we have the list of sender name, and the we get the recipient id and the message to be sent and using the above snippet of code we can send messages from one user to one another user.

On click of the bell icon, the list of notifications of the user appears as a drop-down menu. Further we can make improvements like on click notifications vanish and marking notification as read on click etc, but since our app is a basic version, we display the notifications for each of the user.

We see from the code shown in the snippet, that we can access the list of notifications of an user, directly from the user in the template. And for each of the notification in the list of notifications, we are displaying the information of the sender, the recipient and the message.

We can see an sample drop down messages, which appear on click of the bell icon.

Drop down notifications

Now coming to the part of the display of the user and sending the message from one user to another, we will take the help of the following code snippet:

We display the list of users in an table. When this page is rendered, the values of the user and list of users are also sent. In the line 10 we use if condition to check if the logged in user is same as user in the list so that the user cannot send the message to himself. Next we display the user id, user name and then there is the form field which has a text box and an hidden input field for the user_id along with a submit button for each user as shown below.

List of users, text field for message and send message button.

Finally we see about the two views that are written for our simple app. We again use the code snippet for the same and is shown as below:

One is the index view, another is message view, in the index view we query the list of users present in our database and the request user and is rendered to the template as shown in the line 12 in above code snippet. Since we are not using any other app for authentication, we use try except blocks so that our app breaks gracefully if it has to. And also the user will be notified to login from the admin site to our app if he tries to send the message to another user.

Coming to the message view, the data that is required for sending the notification taken from the request.POST method and we notify the user as shown in the code and in lines 21 to 23. Once the user is notified, then the user is redirected to the index page again for further communication.

We see that lines 9,10 corresponds to the views of index and message, and line 11 is related to the notifications reading. More info can be found in the guide of django-notifications-hq.

To exit from the application, the use again needs to enter the admin url and then logout from the admin url.

Yes, so that’s it what i got to say, any suggestions, queries, comments, improvements etc., please feel free to put them in the comments or approach me directly. Thanks for your patience and time for reading the article.

The codes for the project will be pushed to the git repository and this is the link for the repository. Thank you for reading, hope it would help you some time 😃.

Star Gazers

“If you want to master something, teach it.”

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store