The recent release of Django 1.0 included a full rewrite of the comments framework. Comments have been available in Django for a while but were never properly documented until now.
This article will show you how to adapt and extend the comments framework so that it fits the needs of your application. Why extend it? Well, mainly because the framework does what it says on the box—and nothing more. It allows you to attach comments to any Django object instance but for the rest of the business logic—e.g. regulating who can modify and delete comments—you are on your own.
Also, the current documentation does not cover all features so what I am writing here should hopefully fill a few gaps.
Prerequisites
You need to be familiar with Django. If you’re not, then have a look at the tutorial in the official documentation or alternatively at my previous article on how to get started with Django on Google App Engine.
How comments work
It’s really simple—just skim through the well-written documentation and you should pretty much be able to figure it out.
For example, to show a comment form for an instance of a model called my_model_instance
, you just need two lines of template code:
{% load comments %}
{% render_comment_form for my_model_instance %}
The magic behind the comments framework lies in its use of generic model relations. This is a very powerful (and well-hidden) Django feature that allows your models to have generic foreign keys, meaning they can link to any other model. The comments framework uses this technique to ensure that comments can be attached to an arbitrary model in your application.
The scenario
I will be describing a real-life case from my company web site, Eventseer.net. Eventseer is an event tracker that helps researchers stay informed on upcoming conferences and workshops. It uses the comments framework for two different purposes.
Firstly, registered users can add comments to each event in our database. Secondly, all users can claim a personal profile page where they get what we call a whiteboard—which is simply a blogging application. Each entry on a whiteboard can be commented on by other registered users.
The problem
There are some limitations when it comes to adding comments on Eventseer. For example, only registered users are allowed to add comments. After a comment has been added, only the user who added it or an administrator are allowed to delete it.
These are fairly typical requirements—which are not supported out of the box in the comments framework. There is some support for using the built-in permissions system, but this will still not let you exercise fine-grained per user access control.
Moreover, the default comment templates are ugly as sin and will have to adapted to fit your application.
Step 1: Enabling comments
This is described well enough in the standard documentation. However, if we want to add extra functionality there are a couple of extra things to be done.
First, we add the comments framework to INSTALLED_APPS
in settings.py
:
# eventseer/settings.py
INSTALLED_APPS = (
...
'django.contrib.comments',
'eventseer.mod_comments',
...
)
Note that I also added an app called eventseer.mod_comments
. This is where our comments wrapper code will reside. (I will be using the eventseer
project name for the rest of this tutorial).
Now synchronize the database:
$ python manage.py syncdb
This creates the tables necessary for storing the comments.
Finally, add an entry in your base urls.py
:
# eventseer/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
...
(r'^comments/', include('eventseer.mod_comments.urls')),
)
This is where we deviate from the standard documentation: Instead of routing all comment URLs to the bundled comments application we instead route them to our own custom application. This allows us to intercept comment URLs as required.
Step 2: Add the modified comments application
This is done the usual way:
$ python manage.py startapp mod_comments
In the previous step we added a reference to urls.py
in the mod_comments
application, so this file must be added:
# eventseer/mod_comments/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^delete/(?P<comment_id>\d+)/$', 'eventseer.mod_comments.views.delete'),
(r'', include('django.contrib.comments.urls')),
)
The first line routes requests to /comments/delete/
to a custom delete view which we will create in the next step. For this example this is the only behavior we wish to modify. The last line ensures that all other requests are passed through to django.contrib.comments.urls
.
Step 3: Create the wrapper view
We want to make sure that only the user who wrote a comment or administrators are allowed to delete it. This can be taken care of in mod_comments/views.py
:
# eventseer/mod_comments/views.py
from django.contrib.auth.decorators import login_required
from django.contrib.comments.models import Comment
from django.http import Http404
from django.shortcuts import get_object_or_404
import django.contrib.comments.views.moderation as moderation
@login_required
def delete(request, comment_id):
comment = get_object_or_404(Comment, pk=comment_id)
if request.user == comment.user or \
request.user.is_staff:
return moderation.delete(request, comment_id)
else:
raise Http404
First we wrap the delete function with the login_required
decorator so as to keep out non-authenticated users. We then check if the user who made the delete request actually owns the comment or if the user has administrator permissions. If either case holds true we pass the request on to the original delete
method. Otherwise a 404 (page not found) error is raised.
We can of course modify the view method signature as required. In fact, the original delete method can be completely bypassed if that is what we want.
Step 4: Modifying delete behavior
By default the delete view shows a confirmation page (comments/delete.html
) on GET requests and does the actual deletion on POST requests. After the deletion is done you will be shown the standard deleted.html
template. Alternatively, adding a next
parameter to the POST request will send the user to the given URL.
Say we wish to make some changes to the confirmation page, comments/delete.html
. Instead of modifying the original in the Django distribution we create our own version. Create the directory eventseer/mod_comments/templates/comments
and copy delete.html
into it.
You will typically find this file in /usr/lib/python2.5/site-packages/django/contrib/comments/templates/comments
on Linux systems or C:/Python2.5/Lib/site-packages/django/contrib/comments/templates/comments
on Windows systems—your mileage may vary.
Typically you will wish to change this template to fit in with your site design, for instance by inheriting from your base templates.
To make the modified template take precedence, just add the new directory to settings.py
:
# eventseer/settings.py
TEMPLATE_DIRS = (
...
'/home/eventseer/src/eventseer/mod_comments/templates',
...
)
This will make sure that the Django URL resolver queries the eventseer/mod_comment/templates
directory—where it will find our alternative version of comments/delete.html
. Requests to other comment views that use the other default templates will be passed through to the correct default location.
Conclusion
The Django comments framework is the easiest and quickest way to add commenting functionality to your application. The flip side of this simplicity is that you will often have to extend the framework to make it behave according to your requirements. As this tutorial have shown, this can be done without making changes to the comments framework itself. One of the core strengths of Django is how it provides a set of reusable building blocks upon which you can add your own advanced functionality as required.
At the time of writing, the comments framework documentation is somewhat sparse. If you want to learn more about the inner workings of Django comments you will have to consult the source code—there are quite a few undocumented features that are really useful.
UPDATE
Tim Hoelscher noticed that I hadn’t said anything about how to work around the Django permission system, which was an unintentional omission.
The original delete
method in django.contrib.comments.views.moderation
requires that the user who wants to delete a comment has the comments.can_moderate
permission. Regular users do not have this permission by default, so we have to set it for all users who are allowed to delete comments. (Remember, the wrapper delete
makes sure that they can only delete their own comments.)
An easy way to solve this is to create a ‘user’ group, assign the comments.can_moderate
permission to this group, and finally assign all users to this group. This can be done through the admin interface, with a few lines of SQL, or within your Django application. Refer to the Django permissions documentation for more information on how permissions work.