Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django NameError [app name] is not defined

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.

like image 792
jonathanatx Avatar asked Feb 12 '11 20:02

jonathanatx


4 Answers

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'
like image 72
Yuji 'Tomita' Tomita Avatar answered Nov 02 '22 07:11

Yuji 'Tomita' Tomita


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.

like image 23
strocknar Avatar answered Nov 02 '22 07:11

strocknar


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

from django.conf.urls import include
like image 27
Aditya Avatar answered Nov 02 '22 07:11

Aditya


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'))
like image 29
Yang Wang Avatar answered Nov 02 '22 08:11

Yang Wang