Skip to content

Django tips: Hacking FreeComment

Published: July 16, 2006. Filed under: Django, Programming.

Django’s bundled comments application (found in django.contrib.comments) is incredibly useful; it gives you a nice, out-of-the-box system for adding comments to any site. But, if you look at it closely, really feels more like two applications:

  1. The Comment model and its helpers, which only allows registered user accounts to post comments.
  2. The FreeComment model and its helpers, which allows anyone at all to post comments.

The first one — based around Comment — is much more complex by far, and includes pretty much everything World Online has ever needed for its various news and community sites: reviews, ratings, attached images, reputation over time, flagging of potentially bad content, user bans and groups of moderators who can remove comments. Explaining everything it can do would take several extremely long articles.

The second one — based around FreeComment — is extremely simple, and doesn’t have all the fancy bells and whistles of its big brother; its model is a heavily distilled set of fields, and it mostly just lives to attach basic comment objects to any bit of content you throw at it. There are plenty of use cases where this is ideal, since lots of sites don’t need or want all of the features and administrative overhead that come with the Comment model and the simplicity makes FreeComment pretty easy to understand; a simple one-page tutorial suffices to explain most of the things you’ll need to know to use it.

But then there are cases which fall somewhere in between: for example, if you want to allow anyone to comment without needing an account, but want a little more flexibility in controlling and filtering the comments that get posted and displayed.

Some of that flexibility is baked in, if you know how to use it, but some of it isn’t; in this article I’ll be walking through some tricks you can do with comments, and some modifications you can make to the FreeComment-based system to get some useful extra functionality.

Warning! Some of what I cover here is going to require you to make changes to the code in your copy of Django, which may cause hassles when upgrading or restrict your interoperability with any code that assumes a pristine copy of django.contrib.comments. Think things through and weigh your options carefully before you try any of this on a production application.

Edit one year later: I’d like to re-emphasize the above, and even go a little further. Don’t do what this entry describes. I did, way back before I wrote this up, and I’m now clawing my way back out of it one line of code at a time. I’ve posted an unobtrusive comment-moderation function on djangosnippets, and I’ve got a few other things in the works to replicate the things this entry does, but in ways that don’t touch Django’s code at all. So, again, don’t do any of the things listed below; they’ll just cause you pain.

Edited again: no, really, don’t use this. Check out this generic application which provides comment moderation without having to hack on Django. In light of that, I’ve removed the remainder of this entry.