Django tips: Hacking FreeComment
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:
Commentmodel and its helpers, which only allows registered user accounts to post comments.
FreeCommentmodel 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.