How to upgrade to Django Comments 1.0
The release of Django 1.0 held many new developments for the Python-based framework that I've used to build several projects during the past few months. But one feature I was really looking forward to exploring was the new, refactored comments application.
Unfortunately, Django Comments 1.0 also comes with the scary prospect of database updates, data migrations, markup changes, CSS changes and other headaches. Here's how I tackled the now-documented upgrade for this application.
Prepare your projects
My personal Web site was already running on a variation of Django trunk (pre-alpha) before I upgraded to Django 1.0 final. This made the update process somewhat easier since I'd already been through the updates for newForms admin and the like.
Also, I don't have a typical development environment for this site, which really puts an emphasis on reading through the documentation. You don't want surprises from backwards-incompatible changes!
Here are the steps I took for preparing my site for Django Comments 1.0:
- Read the documentation on the Django Project site thoroughly (including the upgrade guide)
- Comment out, or remove any references to the awesome, but incompatible (for now) django-comment-utils package
- Change any reference to "FreeComment" to "Comment"
-
Change template tags to reflect the new methods of getting the list of comments or the comment count, like
{% get_free_comment_list for blog.entry object.id as comment_list %}and{% get_comment_count for blog.entry object.id as comment_count %} -
Update the template tag for rendering your comment form to
{% render_comment_form for object %} -
In your URLs, change
(r'^comments/', include('django.contrib.comments.urls.comments')),to(r'^comments/', include('django.contrib.comments.urls')), -
If you're providing an RSS feed for the latest comments, change this to
from django.contrib.comments.feeds import LatestCommentFeed
You'll also need to take a look at the new template offerings provided with Django Comments 1.0:
- reply_preview.html
- posted.html
- preview.html
- moderation_queue.html
- flagged.html
- form.html
- flag.html
- reply.html
- base.html
- delete.html
- deleted.html
- approve.html
- approved.html
- 400-debug.html
I had to update my modified versions of preview.html and posted.html to reflect the upgrade.
Once you've prepared your site for the upgrade, run the following, and hopefully familiar, command from shell: python manage.py syncdb
This will install the new "django_comments" table and index in your database.
Migrating your comments to Django Comments 1.0
For those with little knowledge of MySQL, don't fret. This part is very easy thanks to a code snippet provided in the Django Comments 1.0 upgrade guide. Simply use the following shell command to enter your database shell python manage.py dbshell and run the following SQL:
BEGIN;
INSERT INTO django_comments
(content_type_id, object_pk, site_id, user_name, user_email, user_url,
comment, submit_date, ip_address, is_public, is_removed)
SELECT
content_type_id, object_id, site_id, person_name, '', '', comment,
submit_date, ip_address, is_public, approved
FROM comments_freecomment;
INSERT INTO django_comments
(content_type_id, object_pk, site_id, user_id, user_name, user_email,
user_url, comment, submit_date, ip_address, is_public, is_removed)
SELECT
content_type_id, object_id, site_id, user_id, '', '', '', comment,
submit_date, ip_address, is_public, is_removed
FROM comments_comment;
UPDATE django_comments SET user_name = (
SELECT username FROM auth_user
WHERE django_comments.user_id = auth_user.id
) WHERE django_comments.user_id is not NULL;
UPDATE django_comments SET user_email = (
SELECT email FROM auth_user
WHERE django_comments.user_id = auth_user.id
) WHERE django_comments.user_id is not NULL;
COMMIT;
Your comments should be properly migrated into the new comments table.
Make sure you restart Apache after these changes!
New HTML in comments
Previous to this upgrade, Django Comments only provided fields for name, e-mail and the comment itself. New in Django Comments 1.0 are name, e-mail, URL and the comment field.
You'll probably need to change your CSS to hook into the new URL field, as well as the new "submit" options.
Also, keep in mind that the "name" database field has changed from person_name to user_name. This will require a change to your template variables.
Other updates includes a number of spam-prevention features such as the "honeypot" field.
For now, I'm relying on the new spam prevention instead of the django-comment-utils Akismet filter. But I'm quite certain I'll need to find a way to re-integrate this soon.
First impressions of Django Comments 1.0
Though it's only been a few hours since I've launched the update, the new Django Comments app appears to be performing very well. Unless you've hacked the older comments app to provide functionality that doesn't exist in the new version, it would be well-worth an upgrade.
I'd love to hear others' feedback on the new app; feel free to post your thoughts in the comments, or contact me directly.

15 comments
An article about comments, with no comments?
your post is about 6 hours too late to help me :/
I already figgered that stuff out earlier today.
@John S
Looks like I'm going to be No. 3!
@crucialfelix
Apologies. I'm glad you were able to get through the upgrade OK!
Perform perform perform! Not 'preform'.
@Spelling Nazi
Fixed. I hate my keyboard sometimes. Of course, I did write this entry only having consumed one cup of coffee.
At any rate, I hope you got more out of the entry than identifying bad grammar.
Have you noticed the comments list in the admin being ordered in reverse?
Just curious. I haven't seen anything on the google group yet, but since I read through your steps, and they were pretty much the same thing that I did, I thought I would ask...
@ shelbybark
Yes, I have noticed that as well. It's somewhat annoying, since you need to hit the "today" filter to see current comments.
Hi Patrick,
I have done some steps that you wrote before i found your blog :) Before this, i was using Freecomments. Later on, I updated django to 1.0. My comments totally broken. Then I fixed it like you wrote on this blog. But now, I got some problems with new HTML such as 'form.html' and 'posted.html'.
for 'form.html': How to customize the css. I have no idea when i see
{{ field }}. Dont know how to play with it.for 'posted.html' once I used posted.html in freecomment. I can do this
{% if object %}< a href="{{ object.get_absolute_url }}" >View your comment< /a >but
object.get_absolute_urldidnt work out with the new comments 1.0ps. I'm quite new to django.
Best Regard,
Tar
Thanks, Patrick. I went ahead and posted to the Google Group. So, hopefully I'll find an answer soon.
Thanks again!
good post
Just to update, there's a ticket already open on the ordering problem:
http://code.djangoproject.com/ticket/8917
@Tar
The
{{ field }}variable pulls in the various fields used within the comment form itself. If you're using the lazy method, rendering the form via the{% render comment form for object %}you can't do much to customize things. You'll need to use the custom comment for for that.Remember that you can create a "comments" directory in your template directory and override the default Django Comments templates. I've done this for posted.html and preview.html. Just reference the new templates when customizing your own.
For what you're trying to do on posted.html, I'd try this:
@Patrick Beeson
Thank you very much for replying my question.
for the code snippet on 'posted.html'. It really works exactly what I want. :)
I already copied those templates that I want to customize my own from '..\django\contrib\comments\templates\comments' to my site '.\templatescomments\'
and now I can do whatever I want with
in 'form.html'
Thanks in advance,
Tar
Now only exists "comments" the "freecomments" doesn't exists anymore. In some projects I have only comments for authenticated users and I didn't figure it how I remove the "Name", "Email" and "Url" from the forms without hacking the templates preview.html and form.html.
Thank you for writing this up.
Comments no longer accepted for this entry.
To prevent spam, comments are no longer allowed after 60 days.