Python Django NameError [应用程序名称] 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4980297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django NameError [app name] is not defined
提问by jonathanatx
Trying to use django-grappelli for my admin theme, install has been surprisingly challenging. Running into the following in my urls.py:
尝试将 django-grappelli 用于我的管理主题,安装非常具有挑战性。在我的 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'
用 pip 安装了 grappelli,并且 grappelli 在我的站点包目录中。添加到我的INSTALLED_APPS,运行 syncdb,尝试将 grappelli 添加到我的 pythonpath,但没有运气。如果我在 urls.py 中导入 grappelli,错误会更改为AttributeError - 'module' has no attribute 'urls'
Suggestions or any kind of help is highly appreciated.
非常感谢建议或任何形式的帮助。
采纳答案by Yuji 'Tomita' Tomita
Line should read:
行应为:
(r'^grappelli/', include('grappelli.urls'))
includeeither 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
include要么采用 urls 模块的路径,要么它可以是一个返回 url 模式的 python 对象
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: grappelliis not defined, as in.. it hasn't been defined.
至于错误,它是 Python 中要调试的最直接的错误之一:grappelli未定义,例如……尚未定义。
Imagine being in the shell:
想象一下在shell中:
>>> print grappelli
exception: variable undefined
>>> grappelli = 'hello' # we just defined grappelli
>>> print grappelli
'hello'
回答by strocknar
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.
我意识到这已经有一年多了,但是当我遇到同样的问题时,它是 Google 上最好的结果之一。
Rather than importing urlpatterns from grapelli.urls, you can also change the include() statement
除了从gravelli.urls 导入urlpatterns,您还可以更改include() 语句
(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.
这也让我有点失望,直到我注意到需要在 include 语句中引用 package.urls。
回答by Aditya
You might want to import the following in urls.py:
您可能希望在urls.py 中导入以下内容:
from django.conf.urls import include
回答by Yang Wang
When declaring your routes, you forgot to quote an expression.
在声明路由时,您忘记引用表达式。
Replace grappelli.urlsby 'grappelli.urls'to make it work!
更换grappelli.urls用'grappelli.urls',使其工作!
The correct syntax would then be:
正确的语法是:
(r'^grappelli/', include('grappelli.urls'))

