7

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.

Foo Bar
  • 147
  • 2
  • 10
jonathanatx
  • 1,603
  • 3
  • 12
  • 13

4 Answers4

21

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'
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • Thanks @Yuji - I can't believe I didn't try that. It seemed like it should be obvious but I got tripped up when importing grappelli didn't give me any trouble. Was assuming adding grappelli to my installed_apps gave me access to that object as variable. Will accept as soon as it'll let me. – jonathanatx Feb 12 '11 at 20:15
  • NP! I did notice that the grappelli docs suggest you `include(grappelli.urls)` :) wrong. – Yuji 'Tomita' Tomita Feb 12 '11 at 20:23
  • It's important to point out the the quotes around 'grappelli.urls' is what fixes this issue... for whatever reason. – tufelkinder Feb 24 '13 at 04:50
  • Well, I'm not using grappelli, but I have the same error with another package. I tried importing the package and then referencing package.urls but that still did not work until I surrounded it with quotes and then I did not even need to import that package. Regardless, your answer and the one below provided a solution that works. :-) – tufelkinder Feb 24 '13 at 22:33
  • I have encountered similar problem, but my solution is to change "chdir = /var/django/zhanxw/" to "chdir = /var/django/zhanxw" in my uWSGI conf file (removing the trailing slash). Hope this is helpful for someone. – zhanxw Aug 20 '13 at 20:25
  • ty. I forget this and I was surprised when seeing this soloution – Ebrahim Bashirpour Nov 05 '20 at 00:22
11

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.

strocknar
  • 1,146
  • 9
  • 9
2

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

from django.conf.urls import include
Aditya
  • 1,136
  • 2
  • 12
  • 19
2

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'))
Eloims
  • 5,106
  • 4
  • 25
  • 41
Yang Wang
  • 580
  • 4
  • 14