Django NameError [应用程序名称] 未定义

发布于 2024-10-17 06:43:08 字数 502 浏览 4 评论 0原文

尝试使用 django-grappelli 作为我的管理主题,安装非常具有挑战性。在我的 urls.py 中遇到以下内容:

NameError .. name 'grappelli' is not defined

在 Installed grappelli with pip行上抛出错误

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

,并且 grappelli 位于我的sites-packages 目录中。添加到我的 INSTALLED_APPS 中,运行syncdb,尝试将 grappelli 添加到我的 pythonpath 中,但没有成功。如果我在 urls.py 中导入 grappelli,错误将更改为 AttributeError - 'module' has no attribute 'urls'

非常感谢建议或任何类型的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雪化雨蝶 2024-10-24 06:43:08

该行应为:

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

include 要么采用 urls 模块的路径,要么它可以是返回 url 模式的 python 对象
http://docs.djangoproject.com/en/dev/topics/http/urls/# include

因此,你的两个选项要么是上面的行(url 的路径),要么

from grappelli.urls import urlpatterns as grappelli_urls

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

至于错误,它是 Python 中最直接的调试错误之一: grappelli is not Defined,如..尚未定义。

想象一下在 shell 中:

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

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'
没︽人懂的悲伤 2024-10-24 06:43:08

我意识到这已经有一年多了,但当我遇到同样的问题时,它是谷歌上的最佳结果之一。

而不是从 Gratelli.urls 导入 urlpatterns。

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

您还可以将 include() 语句更改为,

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

这也让我有点困惑,直到我注意到需要在 include 语句中引用 package.urls。

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.

jJeQQOZ5 2024-10-24 06:43:08

您可能需要在 urls.py 中导入以下内容:

from django.conf.urls import include

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

from django.conf.urls import include
世界和平 2024-10-24 06:43:08

当声明你的路线时,你忘记引用一个表达式。

grappelli.urls 替换为 'grappelli.urls' 以使其正常工作!

正确的语法是:

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

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'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文