Building a Social Media Site With Python and Django: Part 2 Social Feed and Posts

Video Tutorial

Code on Github

In this tutorial, we will add a social feed where posts can be viewed and a form where a new post can be added, we are not going to worry about the small details in what should be shown to each user yet, for now we will just show all posts. We will come back later and add those extra details to make each feed unique for each user later.

First we need to set up our Post model in our social/models.py:

We are just creating a simple data model here, where we have a body of text, a date that the post was created on, and which user that wrote the post. Now with that done, we can create a view for the social feed. This view will first list all the posts when a get request is sent, there will also be a form at the top to submit a new post, let’s do that first:

In this view, we get all the posts and order them by newest first. You’ll see that we also instantiated a new form, this will be the form to create a new post. Let’s create a forms.py file in the social app to create the PostForm that we imported here in our views:

We will use the ModelForm object for this, the ModelForm will allow us to create a form based on a model. In this case, we will create it based off our Post model. In the Meta subclass we can define the information we need, which will be what model we want this form to be for, and what fields. We only want the body text to be a field in the form, so in our fields variable we create a list and only put ‘body’ inside of it. Above this, we can define specific details about each field. We want to remove the label and set some attributes on the input field. We can do this by setting a variable that is named the same as a field and set the data we need. This will create a text area input with 3 rows by default and some placeholder text.

Now with that created, we can create an html template for this view, make sure to name it the same as what you called it in the render function in the view. In this case, we called it post_list.html:

We can reuse the landing base template that we created last time. At the top we are putting in the form and using crispy forms to make it look good. We can use a for loop to iterate through the posts and display each of them.

Finally, the last step is to create a url route for this view, let’s create a urls.py in our social app:

Now let’s include this in our root urls file:

Now you should be able to view the social feed. But the form won’t work yet, let’s make that work now, first we need to handle the form submission as a post request in our view:

We can do almost the exact same thing that we did in our get request, except now we need to pass in the request.POST data to our form and we can check if the form is valid. If it is, we can set the author to be the current logged in user and save the post. Then we can just render the exact same template and it will show the new post.

With that done, we have a basic social feed created and a way to add new posts. For now we will stop here although we still have some functionality to add. We can’t edit/delete posts and we can’t add comments to posts, we will add this in the next tutorial.