Build a Social Media Website with Django- Part 2 | Towards Data Science

So, in the first part of the tutorial, we learnt how to set up our project and setup various authentication backends and other details regarding various installs and app setups needed in the settings file.
If you have not read the first part yet, make sure to complete it first before moving forward as we would be building on top of that in this part.
There would be lots of terms which you may be encountering for the first time, like you may have heard about views or models for the first time, so I would be briefing about them a little. I cannot talk about those in details because our focus is not to teach you basic concepts, but to build a social media website. So, I would be linking to good resources for you to refer to those specific topics and then continue with this tutorial.
So, let’s go ahead and build the Users app we created in the last tutorial. We will start by creating the models first.
In this python file, we will define our models. As you might already know, models are defined to tell Django what to save in the database and to define the relationship between different models and what characteristics they have.
To learn more about Django models, do visit this amazing tutorial on models by Mozilla Developers. It talks about models in depth.
After you are comfortable with how models work, you can proceed to make models for our social media website.
So, we will have two models — one for the user’s profile and the other for Friend Requests.
We will need to import many things before we begin writing the models. We will be using the Default User model of Django to have One to One Relationship with our Profile Model i.e. each user will have one profile.
We are also using autoslug to automatically produce slugs for the URLs based on the username.
For e.g.: A user with the name Tom would have slug as tom. This will help us make meaningful URLs as users/tom will lead to Tom’s profile rather than numbers.
So, to use autoslug we need to install it first. It can be done via a simple pip install. Refer below:
pip install django-autoslug
Also we will need to install the pillow library to handle the images in Profile model.
pip install pillow
After installing it, we can import it in models.py by using the line:
from autoslug import AutoSlugField
After finishing off all the required imports, we can begin writing the models.
So, our first model is the Profile model. It has five parameters:-
user — This is a One to One Relationship with Django User model. The on_delete=models.CASCADE means on the deletion of User, we destroy the Profile too.image — This will store the profile picture of the user. We have provided a default image also. We need to define where to save the pictures.slug — This will be the slug field. We use the AutoSlugField and will set it to make slug from the user field.bio — This will store the small introduction about the user. Here, blank=True means it can be left blank.friends — This is a Many to Many Field with Profile model and can be left blank. It means every user can have multiple friends and can be friends to multiple people.
user — This is a One to One Relationship with Django User model. The on_delete=models.CASCADE means on the deletion of User, we destroy the Profile too.
image — This will store the profile picture of the user. We have provided a default image also. We need to define where to save the pictures.
slug — This will be the slug field. We use the AutoSlugField and will set it to make slug from the user field.
bio — This will store the small introduction about the user. Here, blank=True means it can be left blank.
friends — This is a Many to Many Field with Profile model and can be left blank. It means every user can have multiple friends and can be friends to multiple people.
Next, we describe the __str__ which decides how Django will show our model in the admin panel. We have set it to show the username as the Query object.
We also define the get_absolute_url to get the absolute URL for that profile.
Next, we define a function to make a profile as soon as we create the user so that the user doesn't have to manually create a profile.
Next, we define our Friends Model. It will have three parameters:-
to_user — This denotes the user to whom the friend request will be sent. It will have the same on_delete parameter which decides when the user is deleted, we delete the friend request too.from_user — This denotes the user who is sending the friend request. It will also be deleted if the user is deleted.timestamp — It is not really necessary to add. It stores the time when the request was sent.
to_user — This denotes the user to whom the friend request will be sent. It will have the same on_delete parameter which decides when the user is deleted, we delete the friend request too.
from_user — This denotes the user who is sending the friend request. It will also be deleted if the user is deleted.
timestamp — It is not really necessary to add. It stores the time when the request was sent.
As you can notice both to_user and from_user uses the same ForeignKey so to differentiate we need to use the related_name field.
So, that finishes our models.py file. Have a look at the code below which shows the models.py file.
After models.py file, we move forward to admin.py file.
It will be short and it will consist of a few lines only. It denotes the models which we will be registering to our admin panel. We will be registering both our models here.
Next, we move to forms.py.
To know more about working of forms in Django, do visit this official tutorial by Django itself. Then proceed into the tutorial.
We define three forms in our forms.py file.
UserRegisterForm — This is for registration of a new user. We user the Django’s default UserCreationForm and we define what should be in the forms. We set the email to be Django’s EmailField. Then we tell Django that model is User and the fields that we would ask the user to fill while registering.UserUpdateForm — This form will let users update their profile. It will have all the same fields as Registration form but we would use the Django Model form instead of UserCreationForm.ProfileUpdateForm — This form will let users update their profile.
UserRegisterForm — This is for registration of a new user. We user the Django’s default UserCreationForm and we define what should be in the forms. We set the email to be Django’s EmailField. Then we tell Django that model is User and the fields that we would ask the user to fill while registering.
UserUpdateForm — This form will let users update their profile. It will have all the same fields as Registration form but we would use the Django Model form instead of UserCreationForm.
ProfileUpdateForm — This form will let users update their profile.
So, adding these three forms would complete our forms.py file. Look at the code below:
So, after this, we have our forms.py file created. Next, we would be seeing views.py file.
Now, we will define the views.py file. It would contain all our views (how to render the files in the web browser). It directly passes data to the templates.
Read this official tutorial by Django to understand views in a better way. After reading the tutorial, we move forward.
Since the views file is too large, we can make it as we like, so I would give a simple overview of what each view does, and you can read the code below for better understanding. So, let’s go through them one by one:
users_list — This view will form the user list to be recommended to any user to help them discover new users to make friends with. We will filter out our friends from that list and will exclude us too. We will make this list by first adding our friend’s friends who are not our friends. Then if our user list has still low members, we will add random people to recommend (mostly for a user with no friends).friend_list — This view will display all the friends of the user.send_friend_request — This will help us create a friend request instance and will send a request to the user. We take in the id the user we are sending a request to so that we can send him the request.cancel_friend_request — It will cancel the friend request we sent to the user.accept_friend_request — It will be used to accept the friend request of the user and we add user1 to user2’s friend list and vice versa. Also, we will delete the friend request.delete_friend_request — It will allow the user to delete any friend request he/she has received.delete_friend — This will delete the friend of that user i.e. we would remove user1 from user2 friend list and vice versa.profile_view — This will be the profile view of any user. It will showcase the friend's count and posts count of the user and their friend list. Also, it would showcase the friend request received and sent by the user and can accept, decline or cancel the request. Obviously, we would add conditions and checks, so that only the concerned user is shown the requests and sent list and they only have the power to accept or reject requests and not anyone viewing his/her profile.register — This will let users register on our website. It will render the registration form we created in forms.py file.edit_profile — This will let the users edit their profile with help of the forms we created.search_users — This will handle the search function of the users. It takes in the query and then filters out relevant users.my_profile — This is same as profile_view but it will render your profile only.
users_list — This view will form the user list to be recommended to any user to help them discover new users to make friends with. We will filter out our friends from that list and will exclude us too. We will make this list by first adding our friend’s friends who are not our friends. Then if our user list has still low members, we will add random people to recommend (mostly for a user with no friends).
friend_list — This view will display all the friends of the user.
send_friend_request — This will help us create a friend request instance and will send a request to the user. We take in the id the user we are sending a request to so that we can send him the request.
cancel_friend_request — It will cancel the friend request we sent to the user.
accept_friend_request — It will be used to accept the friend request of the user and we add user1 to user2’s friend list and vice versa. Also, we will delete the friend request.
delete_friend_request — It will allow the user to delete any friend request he/she has received.
delete_friend — This will delete the friend of that user i.e. we would remove user1 from user2 friend list and vice versa.
profile_view — This will be the profile view of any user. It will showcase the friend's count and posts count of the user and their friend list. Also, it would showcase the friend request received and sent by the user and can accept, decline or cancel the request. Obviously, we would add conditions and checks, so that only the concerned user is shown the requests and sent list and they only have the power to accept or reject requests and not anyone viewing his/her profile.
register — This will let users register on our website. It will render the registration form we created in forms.py file.
edit_profile — This will let the users edit their profile with help of the forms we created.
search_users — This will handle the search function of the users. It takes in the query and then filters out relevant users.
my_profile — This is same as profile_view but it will render your profile only.
Go through the code below for better understanding.
It sums up our Users app. We are left with the urls.py which we won’t include in users app. We will add it directly to our photoshare app.
You can view this simple tutorial to understand more about URLs in Django.
This file contains all the URLs for the website. It has an include(‘feed.urls’) which includes all URLs from the Feed app which we will build in the next tutorial.
And we include all the URLs for the photoshare app directly in the main urls.py file. Have a look at the file below:
If you would like to see the complete code, view the project’s Github Repo. Also, you can try out this app by visiting this link. It is hosted on Heroku and the media and static files on Google Cloud Storage.
A similar Django focused series (Building a Job Search Portal) which will teach you some amazing new concepts is:
shubhamstudent5.medium.com
The next part of this tutorial is:
medium.com
shubhamstudent5.medium.com
towardsdatascience.com
A collection of resources to learn Web Development can be found here:-