COMMENTS_APP error when extending Django comments

In the hope of saving someone else the fruitless hour of Googling I just spent… If you’re trying to customize the Django comments framework as per the example, you might find yourself getting this error when the server tries to start up:

Validating models...
Unhandled exception in thread started by <function inner_run at 0x1007c02a8>
Traceback (most recent call last):
File "/Users/phil/.virtualenvs/django-hines/lib/python2.6/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run
self.validate(display_num_errors=True)

[...several lines snipped...]

File "/Users/phil/.virtualenvs/django-hines/lib/python2.6/site-packages/django/contrib/comments/__init__.py", line 24, in get_comment_app
  raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\
django.core.exceptions.ImproperlyConfigured: The COMMENTS_APP setting refers to a non-existing package.

This might happen even though you seem to have done everything the examples suggest you do, including remembering to set that COMMENTS_APP setting, which is all-too-easily overlooked. And, also, contrary to that error message, you’ve ensured the app referred to in that COMMENTS_APP setting is identical to the name of your custom comments app in INSTALLED_APPS.

The solution to this, which I couldn’t find anywhere online, is to change the order of your INSTALLED_APPS. Dumb, simple, but not very obvious.

Here’s my INSTALLED_APPS that generates the error:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.comments',
    'django.contrib.admin',
    'django.contrib.markup',
    'django.contrib.flatpages',
    'taggit',
    'hines.weblog',
    'hines.aggregator',
    'hines.comments',
)

And here’s a version which works fine:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.comments',
    'django.contrib.admin',
    'django.contrib.markup',
    'django.contrib.flatpages',
    'taggit',
    'hines.comments',
    'hines.weblog',
    'hines.aggregator',
)

The difference? I changed the position of ‘hines.comments’, my custom comments app. It also works in other locations, further up the list, but won’t work after ‘hines.weblog’. Maybe it’s an error in my code elsewhere, some dependency that I don’t understand. Who knows.

But there we go, if you get that error when extending the Django comments framework, try changing the order of your installed apps.

UPDATE: In the example above I’ve called my custom app “hines.comments”. I was worried that the “comments” name would conflict with “django.contrib.comments” but it mostly seems fine. Except… if you have a custom templatetags/comments.py file and load it in a template with {* load comments *}. Then it seems to conflict with the standard comments tags file. Re-naming your file to something like templatetags/comments_tags.py and using {* load comments_tags *} appears to work though. (3 October 2010)

UPDATE 2: I also found that if I added some unittests in comments/tests.py that they wouldn’t run. I eventually discovered that this was fixed if I moved the ‘hines.comments’ line even further up the list in INSTALLED_APPS, to above ‘django.contrib.comments’. Some further conflict I guess. This seemed like it was getting dangerous, so I ended up re-naming my app, to “hines.customcomments”. I still had to put it above ‘hines.weblog’ in the list but the tests now ran OK if it was below ‘django.contrib.comments’. And I still needed to call the template tags file customcomments_tags.py rather than customcomments.py or I got “No module named models” errors. Phew. (4 Oct 2010)

Comments

  • taken from the django docs:

    App names must be unique
    The application names (that is, the final dotted part of the path to the module containing models.py) defined in INSTALLED_APPS must be unique. For example, you can’t include both django.contrib.auth and myproject.auth in INSTALLED_APPS.

2 Oct 2010 at Twitter

  • 08:43am: @antimega Like @megnut said, Strand is *the* bookstore. I also happened across Mercer Books (Mercer & Bleecker), small but quite good.
  • 11:04am: Stern sideways glances to make parents stop their child coughing at me, and walking on a table people eat from. #iblametheparents
  • 01:55pm: Systems down, Waitrose only able to take cash, society reduced to an alternate history 1970s with twenty varieties of olive oil.
  • 08:54pm: Finally got round to watching the rest of 'Grandma's House' today. Very good, episode 5 a particular highlight.