Django 使用反向和 url 标签的奇怪问题
我发现在 Django 1.2.1 中使用反向非常奇怪的事情。
我有:
myapp/
views.py
urls.py
在 urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('myapp.views',
url(r'^$', 'browse'),
)
在views.py
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
print reverse('myapp.views.browse') # <----- this will print correct value
def browse (request):
print reverse('myapp.views.browse') # <----- this fails with exception
return render_to_response('myapp/browse.html')
当我将反向方法放在视图方法之外的任何位置(浏览 - 在本例中)时,我在每次进一步使用反向或 {% url %} 标记时都会遇到异常。
NoReverseMatch at /
Reverse for 'myapp.views.browse' with arguments '()'
and keyword arguments '{}' not found.
搞什么?当我注释/删除 browser() 外部的打印行时, browser() 内部的第二个打印行神奇地开始工作!
最基本的情况是:
class MyForm(forms.Form):
field = forms.CharField(default=reverse(....))
def some_view(request):
print reverse(...)
....
1)我在主范围中定义一个类,该类在 django 初始化时初始化(并反向运行) 2)当请求到来时,some_view 函数被触发,并且它再次评估反向函数(并且失败并出现异常)。
我认为这种方法没有任何坏。为什么不使用reverse()函数的结果初始化django主作用域中的一些值?
I've found very odd thing about using reverse in Django 1.2.1.
I have:
myapp/
views.py
urls.py
in urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('myapp.views',
url(r'^
in views.py
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
print reverse('myapp.views.browse') # <----- this will print correct value
def browse (request):
print reverse('myapp.views.browse') # <----- this fails with exception
return render_to_response('myapp/browse.html')
When I put reverse method anywhere outside the view method (browse - in this case) I get an exception in every further use of reverse or {% url %} tag.
NoReverseMatch at /
Reverse for 'myapp.views.browse' with arguments '()'
and keyword arguments '{}' not found.
WTF? When I comment/delete the print line outside browse() , second print line inside browse() magically start working!
The most basic case is:
class MyForm(forms.Form):
field = forms.CharField(default=reverse(....))
def some_view(request):
print reverse(...)
....
1) I define a class in main-scope that is initialized when django initialize (and runs reverse)
2) When a request comes the some_view function has been triggered and it evaluates the reverse function again (and fails with exception).
I don't see anything bad at all in this approach. Why not to initialise some values in the django main-scope with results of the reverse() function ?
, 'browse'),
)
in views.py
When I put reverse method anywhere outside the view method (browse - in this case) I get an exception in every further use of reverse or {% url %} tag.
WTF? When I comment/delete the print line outside browse() , second print line inside browse() magically start working!
The most basic case is:
1) I define a class in main-scope that is initialized when django initialize (and runs reverse)
2) When a request comes the some_view function has been triggered and it evaluates the reverse function again (and fails with exception).
I don't see anything bad at all in this approach. Why not to initialise some values in the django main-scope with results of the reverse() function ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在视图函数中调用reverse() 后,您可能需要将“request”作为第二个参数传递。
这确实是奇怪的行为,但这可能是目前的解决方案。
You will probably need to pass 'request' as the second parameter when calling reverse() from within the view function after it's already been called.
This is odd behavior indeed, but this might possibly be a solution for now.
首先,您应该命名 URL 以便使用反向。这是正确的方法 AFAIK。
其次,为什么要从 FormField 中调用反向?我真的不明白。
也许您可以通过发布完整的代码而不是一组精选的代码片段来启发我们。
First, you should be naming your URLs in order to use reverse. That is the correct approach AFAIK.
Second, why are you calling reverse from within a FormField? I really don't get it.
Maybe you could enlighten us by posting the full code rather than a curated set of snippets.
该问题是由 python 中的 import hell 引起的。反向依赖于其他东西,并且在初始化时不能使用。
解决方案是使用惰性反向。例如使用这个:
http://djangosnippets.org/snippets/499/
The problem is caused by import hell in python. reverse depends on other things and cannot be used while initialization.
The solution is to use lazy reverse. E.g. using this:
http://djangosnippets.org/snippets/499/