Creating a comment system
You will build a comment system wherein users will be able to comment on posts. To build the comment system, you need to do the following:
- 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 a new comment to the database
- Edit the post detail template to display the list of comments and the form to add a new comment
Building a model
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...