'admin' is not a registered namespace in Django 1.4

Rob Golding picture Rob Golding · Mar 26, 2012 · Viewed 15.9k times · Source

I'm attempting to upgrade quite a large Django project to the newly released Django 1.4, and I'm having some issues when running python manage.py test.

Lots of the internal tests which passed in Django 1.3 are now failing, with really odd messages that I can't seem to fix. One that appears the most is:

NoReverseMatch: u'admin' is not a registered namespace

This is raised for the django.contrib.auth tests for password changing in particular (one of which is test_password_change_fails_with_mismatched_passwords (django.contrib.auth.tests.views.ChangePasswordTest). The strange thing is, the namespace is registered correctly and the application functions just fine. I am importing admin in the "new" way:

url(r'^admin/', include(admin.site.urls)),

When I Google this error in particular, all I can find is people importing the admin URLs using the old scheme, and nothing relating to this issue at all.

I've tried removing apps from INSTALLED_APPS one by one, but the auth tests simply won't pass. Also, when I load a Python interpreter from python manage.py shell and execute reverse('admin:index') the URL resolves to /admin/ with no errors. I've read through the code extensively, and can't see where this can be falling down.

As I mentioned earlier, this isn't the only error that's occurring. I'm also getting AttributeError: AUTH_PROFILE_MODULE from the test_site_profile_not_available (django.contrib.auth.tests.models.ProfileTestCase) test, even though AUTH_PROFILE_MODULE is defined in my settings.py file. How can Django's own tests be failing like this?

Answer

Amir Ali Akbari picture Amir Ali Akbari · May 20, 2013

Short Answer: You have a copy of Django admin template files copied in on of your app's templates directory from an earlier version of Django, then you upgraded Django but didn't update (re-copy) those local templates.

Long Answer: The main cause of this problem is using an older version of Django admin template files (which are installed where django itself is installed, usually python's site-packages or dist-packages directory). There is a backward incompatible change in Django 1.5 about url template tag, in which the first parameter must be a string, from Django 1.5 release notes:

One deprecated feature worth noting is the shift to “new-style” url tag. Prior to Django 1.3, syntax like {% url myview %} was interpreted incorrectly (Django considered "myview" to be a literal name of a view, not a template variable named myview). Django 1.3 and above introduced the {% load url from future %} syntax to bring in the corrected behavior where myview was seen as a variable.

So, the problem is you have a copy of admin's template files in one of your app's templates folder, which is coppied from an earlier version of Django. This is usually done for overriding default admin templates. Because of the backward incompatible change noted, these outdated template file can not load in a newer Django environment, and cause the strange error: NoReverseMatch: u'admin' is not a registered namespace.

Changing order of TEMPLATE_LOADERS entries will ignore the local admin templates modifications in favor of default templates file (because default Django templates are loaded by complete path with filesystem.Loader). If the modifications are needed (which is usually the case), you must update your local admin template files from your new Django installation templates and reapply your modifications on them.

Note 1: A similar situation is when local admin templates are newer than Django installation's default, which seems this is your case. Similarly, the better fix is to update all copies of admin templates.

Note 2: Another possibility of getting such error is when using virtualenv. For example, if you are running your project with a virtualenv but the TEMPLATE_DIRS entry for Django admin templates is to your global python installation, you may get this error.