Django NameError [application name] not defined

Trying to use django-grappelli for my admin theme, installation was an amazingly difficult task. By running the following in my urls.py:

NameError .. name 'grappelli' is not defined 

The error is displayed on the line

 (r'^grappelli/', include(grappelli.urls)) 

Grappelli with pip is installed, and grappelli is in my package sites directory. Added to my INSTALLED_APPS , launched syncdb, tried to add grappelli to my pythonpath, but no luck. If I import grappelli into urls.py, the error will change to AttributeError - 'module' has no attribute 'urls'

Suggestions or any help are welcome.

+6
python django django-grappelli
source share
4 answers

The line should read:

 (r'^grappelli/', include('grappelli.urls')) 

include either takes the path to the urls module, or it can be a python object that returns url patterns http://docs.djangoproject.com/en/dev/topics/http/urls/#include

So, your two options are either the line above (the path to the URLs), or

 from grappelli.urls import urlpatterns as grappelli_urls (r'^grappelli/', include(grappelli_urls)), 

As for the error, this is one of the most direct errors in Python for debugging: grappelli not defined, as in .. it was not defined.

Imagine you are in a shell:

 >>> print grappelli exception: variable undefined >>> grappelli = 'hello' # we just defined grappelli >>> print grappelli 'hello' 
+17
source share

I understand that this is more than a year, but it was one of the best results on Google when I had the same problem.

Instead of importing urlpatterns from grapelli.urls, you can also modify the include () statement

 (r'^grappelli/', include(grappelli.urls)) 

to

 (r'^grappelli/', include('grappelli.urls')) 

This also threw me away until I noticed the need to quote package.urls in the include statement.

+10
source share

You might want to import the following into urls.py :

 from django.conf.urls import include 
+1
source share

When declaring your routes, you forgot to specify an expression.

Replace grappelli.urls with 'grappelli.urls' to make it work!

The correct syntax will be as follows:

 (r'^grappelli/', include('grappelli.urls')) 
+1
source share

All Articles