Building a Social Media App With Python 3 and Django Beginners Tutorial Finishing DMs

Video Tutorial

Code on Github

In this tutorial, we will add some extra features to our direct messages. We will add a notification when a user receives a message, we will add an alert if a thread is created with a username that doesn’t exist. We will also add the ability to upload images inside of messages as well. Finally, we will change some CSS styles as well. Let’s get started by adding a notification when a message is created.

Adding a Notification for Messages

First we need to add another notification type, we will need to add a thread field and we will add a comment to keep track of the notification types. We can make a notification inside of our CreateMessage view to send it after the user sends a message. We need to send this to the receiving user on the message.

Now that we have our view set up, we need to update our template tag in the show_notifictations.html. We will need to add another type in an elif after we check for a post. We can then add some text to alert the user that another user sent them a message.

Adding a User Not Found Message

If a user tries creating a thread with a user that doesn’t exist, right now we just redirect back to the same page to have them try again, we need to give them some feedback so they know that it failed. We will use the Django Messages Framework to handle this easily. The messages framework will automatically handle creating the message and removing it once the user sees it. So all we need to do is create a message when we want to and then add a place in our template to check for messages. First let’s create a message after the user enters an invalid username. This will be done inside of our CreateThread view in the post method.

Next let’s check for messages in our templates and display them as a bootstrap alert.

Adding Image Uploads in DMs

Right now we can only upload text in our messages, let’s add the ability to add images as well. This is going to work similar to how we added images to posts. We will need to have a form with an image field and we will need to update our views.py to handle saving the image. We also need to update our template as well.

We will change our MessageForm to a ModelForm to make it easy to save.

Next we need to update our views to save this form data in the database.

The last piece to this is that we need to update our template to show the image. I’m not going to put the code here for that since it is a large file but it will be available in the github repository at the top of this article. That code will look very similar to the post code except we have some extra CSS styles to put the messages and images on the left or right.

Adding the Mail Icon

Finally, we will add an icon to the inbox next to each username on the thread. Right now it just looks a little plain and this will just add a little more to the page.

That is it for this tutorial, we will come back to add more later but this will be a good stopping point for now.