Django Blog #23: Creating a Commenting System

by Alex
Django Blog #23: Creating a Commenting System
Now it’s time to create a blog commenting system so that people can share what they’ve read. To do this, you’ll need to do the following steps:

  1. Create a model for saving comments.
  2. Create a form for submitting comments and checking the entered data.
  3. Add a view that will process the form and save the comment in the system.
  4. Edit the post template to display a list of comments and a form to add a new one.

First, create a model for saving comments. Open the models.py file in the 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(default=True) 
      
   class Meta 
        ordering = ('created',) 
          
   def __str__(self) 
       return 'Comment by {} on {}'.format(self.name, self.post)

This is the Comment model. It contains a ForeignKey to associate with a specific post. This many-to-one relationship is defined in the Comment model because each comment is for one entry, but a post can have multiple comments. The related_name attribute allows you to name the attribute used to associate objects. Once it is defined, the post for which the comment is left can be retrieved using comment.post. All comments can be retrieved with post.comments.all(). If this attribute is not defined, Django will use the model name in small letters and __set (for example, comment_set). This will be the name of the linked object manager. You can read more about linking many to one here: https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_one/. There is a boolean field active which is used to manually disable unacceptable comments. And the created field is for sorting comments in chronological order by default. The new comment model is not synchronized with the database. Run the following command to create a migration, which will mark the creation of the new model:

python manage.py makemigrations blog

The following output appears:

Migrations for 'blog' 
  blog/migrations/0002_comment.py 
    - Create model Comment

Django has generated file 0002_comment.py in the migrations/application blog folder. Now you need to create the linked database schema and apply the changes to the database. Use the command to apply existing migrations:

python manage.py migrate

The output will include the following line:

Applying blog.0002_comment... OK

The migration was applied and the blog_comment table exists in the database. You can now add a new model to the admin site to manage comments through a simple interface. Open admin.py of the blog application, import the Comment model and add the following ModelAmdin class:

@admin.register(Comment) 
class CommentAdmin(admin.ModelAdmin) 
    list_display = ('name', 'email', 'post', 'created', 'active') 
    list_filter = ('active', 'created', ' updated') 
    search_fields = ('name', 'email', 'body')

Run the development server with python manage.py runserver and open the link https://127.0.0.1:8000/admin/ in your browser. You will see that the new model appears in the BLOG section, as in the screenshot: Django Blog #23: Creating a Commenting SystemThe model is registered in the administrative site, which means that Comment instances can be managed with a simple interface.

Related Posts

LEAVE A COMMENT