Part 1: Django Comments for Authenticated Users

Wed 19 November 2008 by Thejaswi Puthraya

Part 1: Django Comments for Authenticated Users

Off late, I have not been hanging in the #django channel...but got an opportunity to do so a couple of days back. A user (I forgot his IRC nick), wanted to use django.contrib.comments to accept comments from authenticated users only.

I suggested that he write his own form template that used the request.user.is_authenticated in an if tag. He was ok with it but didn't want the user to enter his name and email id again. Then I suggested he use hidden fields and he started talking of form spoofing and other advanced techniques. Almost giving up hope (I wonder how Magus- manages to keep his cool), I asked him to write a wrapper for post_comment that would get his job done. The user finally dropped the bomb by telling he was new to python and didn't know what a wrapper was.

Realizing that many folks use django because it makes their life easy and let's them get away without knowing too much of python, I decided to write a step-by-step procedure for accepting comments from authenticated users only.

I am writing this post as a series so that each post is not too boring or intimidating.

First few steps:

The first few steps are mundane and you can ignore these if you know how to setup a django project, the database and a test application.

Note

I use Fedora as my operating system and some steps and applications might not be available in your setup.

  • Start a django project.
[theju@localhost ~]$ django-admin.py startproject comments_reg_users
[theju@localhost ~]$ cd comments_reg_users
  • Set the database by editing the settings.py in the comments_reg_users directory.
import os
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'abc.db'
MEDIA_ROOT = os.path.join(os.getcwd(),'site_media')
TEMPLATE_DIRS = (
    os.path.join(os.getcwd(),'templates'),
)
TEMPLATE_CONTEXT_PROCESSORS=(
   "django.core.context_processors.auth",
   "django.core.context_processors.request",
   "django.core.context_processors.media"
)
  • Add the following to the INSTALLED_APPS in the settings.py.
'django.contrib.admin',
'django.contrib.comments',
'test_app',
  • Create two directories namely templates and site_media in the comments_reg_users directory.
  • Create a new application (or use an existing one if you have).
[theju@localhost comments_reg_users]$ python manage.py startapp test_app
[theju@localhost comments_reg_users]$ cd test_app/
[theju@localhost test_app]$ ls
__init__.py  models.py  views.py
  • Create a model against which the comments will be tied. Let's do a mini photo gallery.
[theju@localhost test_app]$ emacs models.py
from django.db import models

# Create your models here.
class Photo_Gallery(models.Model):
    name = models.CharField(max_length=50)
    pic  = models.ImageField(upload_to='pics')

    def __unicode__(self):
        return self.name

Note

If you don't understand what we've done so far, it is recommended that you go through the django docs.

  • Edit the urls.py in the comments_reg_users directory like below.
from django.conf.urls.defaults import *
import os

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
  (r'^admin/(.*)', admin.site.root),
  (r'^comments/', include('django.contrib.comments.urls')),
  (r'^names/$', list_detail.object_list,
                dict(queryset=Photo_Gallery.objects.all())),
  (r'^names/(?P<object_id>\d+)/$', list_detail.object_detail,
                                   dict(queryset=Photo_Gallery.objects.all())),
  (r'^images/(?P<path>.*)$', 'django.views.static.serve',
                             {'document_root':  \
                               os.path.join(os.path.dirname(__file__),'site_media/')}),

)

We will be using generic views to display the data bound to the models ie the photos in our mini photo gallery. Nothing extra to be written. With this we are done with the first few steps.

Read the next part here.