Django NameError [app name] is not defined

jonathanatx picture jonathanatx · Feb 12, 2011 · Viewed 34.7k times · Source

Trying to use django-grappelli for my admin theme, install has been surprisingly challenging. Running into the following in my urls.py:

NameError .. name 'grappelli' is not defined

The error is thrown on the line

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

Installed grappelli with pip, and grappelli is in my sites-packages directory. Added to my INSTALLED_APPS, ran syncdb, tried adding grappelli to my pythonpath, but no luck. If I import grappelli in urls.py the error changes to an AttributeError - 'module' has no attribute 'urls'

Suggestions or any kind of help is highly appreciated.

Answer

Yuji 'Tomita' Tomita picture Yuji 'Tomita' Tomita · Feb 12, 2011

Line should read:

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

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

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

from grappelli.urls import urlpatterns as grappelli_urls

(r'^grappelli/', include(grappelli_urls)),

As for the error, it's one of the most straight forward errors in Python to debug: grappelli is not defined, as in.. it hasn't been defined.

Imagine being in the shell:

>>> print grappelli
exception: variable undefined
>>> grappelli = 'hello' # we just defined grappelli
>>> print grappelli
'hello'