Building a Social Media App Python 3 & Django Beginners Tutorial Part 17: Sharing Posts

Video Tutorial

Code on Github

In this tutorial we will be adding the ability to share another users post. To do this we will add more fields to our Post model and we will update our forms, views and templates to handle this new feature. Let’s get started by adding to our models.py.

Adding Additional Fields to the Post Model

We need to add some additional fields to handle if a user is sharing another post, to do this we want to create a new post with the original post’s information but also include the sharing user, the date it was shared, and if they entered some text with it, we want to show that as well. So we will create fields for all of these different pieces of data. We will also handle the ordering here in the model instead of using the order_by() method that we were using in our views. This will allow us to order it by multiple fields, we can now order it by the original date but also by the shared date. This will show the shared posts in order by when they were shared.

Creating a Form for Sharing Posts

We will need to create a new form to handle the ability for a user to enter some text with the shared post. This will just be a regular form with one text field.

Adding a View to Handle the Post Request When Sharing a Post

First things first, we can remove all order_by method calls on any Post objects since we handled that in the models. Next we will need to create a view with a post method to handle when the share form is submitted. This will create the new post with all of the data and then it will redirect back to the post list.

We also need to add the share form to both the get and post methods in the post list view.

And then add it to the context dictionary.

Adding the Share Form to the Post List Template

We can add the share form like we have with the others, using crispy forms and the shareform passed into the context.

Adding the button toggle the Share Form

We also want to add a button toggle the share form from shown/hidden like we did for the comment form. We will add the Javascript for this next.

Adding the Javascript to Toggle the Share Form

This will look very similar to the previous toggle function. We are setting the post id on the form, so we can use that to add or remove the d-none class. The d-none class will hide the element from view by setting the CSS style of display: none; on the element.

Updating the Other Templates to Show Shared Posts

To make shared posts show up on the other templates like the post detail or the profile, you can copy over the same logic done on the post list template to those as well.