django url反向不适用于命名url(重新编辑)找到了原因,但现在卡住了!
请参阅底部了解最新突破: 我正在运行 django SVN 15632
在模块 core.views 中尝试了 viewHallo。错误是:'module'对象没有属性'viewHallo'
是我在尝试reverse('home')
或reverse('admin:index')后遇到的错误
。
这是我的项目 urls.py:
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.views import serve as serveStatic
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^dbrowse/', include('dbrowse.urls')),
(r'^static/', serveStatic),
url (r'^$', 'core.views.viewHallo',name='home'),
)
这是
from django.shortcuts import render_to_response
from django.template.context import RequestContext
import site
def viewHallo (request):
pass
return render_to_response ('core.html',
{'site':site,
'title':'i am the hallo view',
'content':'Hallo World!',},
context_instance=RequestContext(request))
使用 shell 甚至脚本 reverse('home')
和 reverse('admin:index')
只是不起作用。
但是,在我的模板 {% url home %}
和 {% url admin:index %} 中工作得很好......
在我的应用程序 core
中,我有一个名为 site.py
我使用此文件来存储有关网站的内容,因此我不必依赖数据库。长话短说,它包含 reverse('home')
。现在这很重要,因为无论 reverse()
在哪里执行脚本、shell 或模板,堆栈跟踪始终包含来自 site.py
的行。
django到底为什么要执行site.py
?即使它确实如此,为什么它会在 reverse('home')
上绊倒呢?有趣的是,如果我注释掉 site.py
中的行,那么 reverse()
就会开始正常工作。
这是怎么回事??这是 core\site.py
from django.contrib.sites.models import Site
from django.conf import settings
from django.core.urlresolvers import reverse
site = Site.objects.get(pk=settings.SITE_ID)
NAME = site.name
SLOGAN = 'it\'s a deal!'
COPY_HOLDER = 'My Name'
#(link_title, 'link_address', ['permission'])
MAIN_MENU = [['home', reverse('home'), 'core.view_tender'],
['admin', reverse('admin:index'), 'is_staff']]
编辑:我已经隔离了 django 源代码中引发错误的行: django/core/urlresolvers.py
第 91 行
91:lookup_view = getattr(import_module(mod_name), func_name)
它会触发 django importlib
,而 django importlib
又会导入 site.py
。
See bottom for latest break throughs:
I'm running django SVN 15632
Tried viewHallo in module core.views. Error was: 'module' object has no attribute 'viewHallo'
is the error I am getting after trying reverse('home')
or reverse('admin:index')
.
this is my projects urls.py:
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.views import serve as serveStatic
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^dbrowse/', include('dbrowse.urls')),
(r'^static/', serveStatic),
url (r'^
this is core\views.py
from django.shortcuts import render_to_response
from django.template.context import RequestContext
import site
def viewHallo (request):
pass
return render_to_response ('core.html',
{'site':site,
'title':'i am the hallo view',
'content':'Hallo World!',},
context_instance=RequestContext(request))
using the shell or even a script reverse('home')
and reverse('admin:index')
just isn't working.
However in my templates {% url home %}
and {% url admin:index %} work just fine...
in my app core
i have a file called site.py
i use this file to store stuff about the site so i dont have to rely on the database. long story short it contains reverse('home')
. Now this is only important because no matter where reverse()
is excecuted script or shell or template, the stack trace always includes the line from site.py
.
why on earth is django excecuting site.py
? and even if it does why is it stumbling over the reverse('home')
?. Interstingly if I comment out the line from site.py
then reverse()
starts working properly.
whats going on?? here is core\site.py
from django.contrib.sites.models import Site
from django.conf import settings
from django.core.urlresolvers import reverse
site = Site.objects.get(pk=settings.SITE_ID)
NAME = site.name
SLOGAN = 'it\'s a deal!'
COPY_HOLDER = 'My Name'
#(link_title, 'link_address', ['permission'])
MAIN_MENU = [['home', reverse('home'), 'core.view_tender'],
['admin', reverse('admin:index'), 'is_staff']]
EDIT: I have isolated the line in django's source code that's throwing the error:
line 91 in django/core/urlresolvers.py
91: lookup_view = getattr(import_module(mod_name), func_name)
it triggers django importlib
which in turn imports site.py
.
, 'core.views.viewHallo',name='home'),
)
this is core\views.py
using the shell or even a script reverse('home')
and reverse('admin:index')
just isn't working.
However in my templates {% url home %}
and {% url admin:index %} work just fine...
in my app core
i have a file called site.py
i use this file to store stuff about the site so i dont have to rely on the database. long story short it contains reverse('home')
. Now this is only important because no matter where reverse()
is excecuted script or shell or template, the stack trace always includes the line from site.py
.
why on earth is django excecuting site.py
? and even if it does why is it stumbling over the reverse('home')
?. Interstingly if I comment out the line from site.py
then reverse()
starts working properly.
whats going on?? here is core\site.py
EDIT: I have isolated the line in django's source code that's throwing the error:
line 91 in django/core/urlresolvers.py
91: lookup_view = getattr(import_module(mod_name), func_name)
it triggers django importlib
which in turn imports site.py
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些问题。
reverse('home')
应该完全按照您的方式工作,但您的错误是在反向完成之前发生的。我们必须首先解决 URL 配置问题。core.views.viewHallo 真的存在吗?
对于管理 url,
name=
将不起作用,因为它是一个包含(没有特定的 url,它只是指向另一个 url conf,所以reverse('admin')
不起作用)。管理 URL 具有命名空间,命名空间 URL 与
namespace:named_url
反转http://docs.djangoproject.com/en/dev/ topic/http/urls/#url-namespaces
你说这是唯一有效的东西 - 你还想做什么?要反转其他管理 URL,这里列出了一个特定的语法:
http://docs.djangoproject.com/en/ dev/ref/contrib/admin/#reversing-admin-urls
如果您有任何疑问,请告诉我。
There are a few problems here.
reverse('home')
should work exactly how you have it but your error is happening before reverse can complete. We've got to sort out the URL conf issues first.Does
core.views.viewHallo
actually exist?For the admin urls,
name=
will not work because it is an include (which doesn't have a specific url, it just points to another url conf, soreverse('admin')
wouldn't work).The admin urls have a namespace, and namespaced urls are reversed with
namespace:named_url
http://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces
You say it's the only thing that works -- what else are you trying to do? To reverse other admin urls, there is a specific syntax outlined here:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls
Let me know if you have any questions.
问题是您在定义函数
viewHallo
之前导入site
模块。错误消息非常正确 - 在调用reverse
时,模块没有这样的成员viewHallo
。通常,模块会在需要时加载,一切都会好起来,但在这种情况下,模块已经在加载过程中。这是 Python 模块之间存在循环依赖的情况。The problem is that you are importing the
site
module before the functionviewHallo
is defined. The error message is quite correct - at the timereverse
is being called, the module has no such memberviewHallo
. Normally, the module would be loaded up when needed and everything would be fine, but in this case, the module is already in the process of being loaded. It's a case of having a cyclic dependency between Python modules.