Creating a comment system
Now, we will build a comment system for the blog, wherein the users will be able to comment on posts. To build the comment system, you will need to do the following steps:
- Create a model to save comments
- Create a form to submit comments and validate the input data
- Add a view that processes the form and saves the new comment to the database
- Edit the post detail template to display the list of comments and the form to add a new comment
First, let's build a model to store comments. Open the models.py
file of your blog
application and add the following code:
class Comment(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments') name = models.CharField(max_length=80) email = models.EmailField() body = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) active = models.BooleanField...