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.

Read full article at “Entries for Django tag | patrickbeeson.com”

Leave a comment