Django - Comments


Before starting, note that the Django Comments framework is deprecated, since the 1.5 version. Now you can use external feature for doing so, but if you still want to use it, it's still included in version 1.6 and 1.7. Starting version 1.8 it's absent but you can still get the code on a different GitHub account.

The comments framework makes it easy to attach comments to any model in your app.

To start using the Django comments framework −

Edit the project file and add 'django.contrib.sites', and 'django.contrib.comments', to INSTALLED_APPS option −

INSTALLED_APPS += ('django.contrib.sites', 'django.contrib.comments',)

Get the site id −

>>> from django.contrib.sites.models import Site
>>> Site().save()
>>> Site.objects.all()[0].id

Set the id you get in the file −

SITE_ID = u'56194498e13823167dd43c64'

Sync db, to create all the comments table or collection −

python syncdb

Add the comment app’s URLs to your project’s −

from django.conf.urls import include
url(r'^comments/', include('django.contrib.comments.urls')),

Now that we have the framework installed, let's change our hello templates to tracks comments on our Dreamreal model. We will list, save comments for a specific Dreamreal entry whose name will be passed as parameter to the /myapp/hello URL.

Dreamreal Model

class Dreamreal(models.Model):

   website = models.CharField(max_length = 50)
   mail = models.CharField(max_length = 50)
   name = models.CharField(max_length = 50)
   phonenumber = models.IntegerField()

   class Meta:
      db_table = "dreamreal"

hello view

def hello(request, Name):
   today =
   daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
   dreamreal = Dreamreal.objects.get(name = Name)
   return render(request, 'hello.html', locals())

hello.html template

{% extends "main_template.html" %}
{% load comments %}
{% block title %}My Hello Page{% endblock %}
{% block content %}

   Our Dreamreal Entry:
   <p><strong>Name :</strong> {{}}</p>
   <p><strong>Website :</strong> {{}}</p>
   <p><strong>Phone :</strong> {{dreamreal.phonenumber}}</p>
   <p><strong>Number of comments :<strong> 
   {% get_comment_count for dreamreal as comment_count %} {{ comment_count }}</p>
   <p>List of comments :</p>
   {% render_comment_list for dreamreal %}

{% render_comment_form for dreamreal %}
{% endblock %}

Finally the mapping URL to our hello view −

url(r'^hello/(?P<Name>\w+)/', 'hello', name = 'hello'),


  • In our template (hello.html), load the comments framework with − {% load comments %}

  • We get the number of comments for the Dreamreal object pass by the view − {% get_comment_count for dreamreal as comment_count %}

  • We get the list of comments for the objects − {% render_comment_list for dreamreal %}

  • We display the default comments form − {% render_comment_form for dreamreal %}

When accessing /myapp/hello/steve you will get the comments info for the Dreamreal entry whose name is Steve. Accessing that URL will get you −

Django Comments Example

On posting a comment, you will get redirected to the following page −

Comments Redirected Page

If you go to /myapp/hello/steve again, you will get to see the following page −

Number of Comments

As you can see, the number of comments is 1 now and you have the comment under the list of comments line.

Useful Video Courses


Build a Stock Market Web App With Python and Django

Best Seller

39 Lectures 3.5 hours

John Elder


Build A Flashcard Website With Python and Django

36 Lectures 2.5 hours

John Elder


Build a Weather App With Python and Django

28 Lectures 2 hours

John Elder


Build An API With The Django Rest Framework Using Python

Best Seller

20 Lectures 1 hours

John Elder


Build A Dental Website With Python and Django

Most Popular

35 Lectures 3 hours

John Elder


Build Python Django Real Project: Django Web Development

Best Seller

79 Lectures 10 hours

Rathan Kumar