O'Reilly logo

Django By Example by Antonio Melé

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a comment system

Now we are going to 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:

  • 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 into the database
  • Edit the post detail template to display the list of comments and the form for adding 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, related_name='comments') name = models.CharField(max_length=80) email = models.EmailField() body = models.TextField() created ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required