Django NameError [app name] is not defined

38.2k views Asked by At

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.

4

There are 4 answers

6
Yuji 'Tomita' Tomita On BEST ANSWER

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'
0
Yang Wang On

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

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

The correct syntax would then be:

(r'^grappelli/', include('grappelli.urls'))
0
strocknar On

I realize this is over a year old, but it was one of the top results on Google when I was having this same problem.

Rather than importing urlpatterns from grapelli.urls, you can also change the include() statement

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

to

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

This threw me off for a bit as well until I noticed the need for quoting the package.urls in the include statement.

0
Aditya On

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

from django.conf.urls import include