Django 使用反向和 url 标签的奇怪问题

发布于 2024-09-19 02:00:48 字数 1280 浏览 3 评论 0原文

我发现在 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 技术交流群。

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

发布评论

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

评论(3

已下线请稍等 2024-09-26 02:00:48

在视图函数中调用reverse() 后,您可能需要将“request”作为第二个参数传递。

def browse(request):
    print reverse('myapp.views.browse', args=[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.

def browse(request):
    print reverse('myapp.views.browse', args=[request])

This is odd behavior indeed, but this might possibly be a solution for now.

还在原地等你 2024-09-26 02:00:48

首先,您应该命名 URL 以便使用反向。这是正确的方法 AFAIK。

其次,为什么要从 FormField 中调用反向?我真的不明白。

也许您可以通过发布完整的代码而不是一组精选的代码片段来启发我们。

# urls.py

url(r'^/
, 'home_view', name='home'),
url(r'^login/
, 'login_view', name='login'),


# views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect

def login_view(request):
    # do login stuff and redirect to home
    return HttpResponseRedirect(reverse('home'))

def home(request):
    # do home stuff

    return render_to_response("home.html", locals(), context_instance=RequestContext(request))

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.

# urls.py

url(r'^/
, 'home_view', name='home'),
url(r'^login/
, 'login_view', name='login'),


# views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect

def login_view(request):
    # do login stuff and redirect to home
    return HttpResponseRedirect(reverse('home'))

def home(request):
    # do home stuff

    return render_to_response("home.html", locals(), context_instance=RequestContext(request))
薄凉少年不暖心 2024-09-26 02:00:48

该问题是由 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/

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