了解django.shortcuts.redirect [英] Understanding django.shortcuts.redirect
问题描述
我有几个问题,了解重定向
或者相反反向
真的有效。
在主要的urls.py中我有:
从django.conf.urls导入模式, include,url
pre>
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
url(r'^ $',redirect_to,{'url' :'/ monitor /'}),
url(r'^ monitor /',include('monitor.urls')),
)
和
monitors.urls
我有:from django.conf.urls import patterns,include,url
urlpatterns = patterns('monitor.views',
(r'^ $ ','index'),
(r'^ abc /(?P< id> * $)','abc'),
)
当您致电
/ monitor
时,我想将其重定向到/ monitor / abc
所以我做了:def index(request):
return redirect abc)
def abc(render,id = None):
return render_to_response(monitor / list.htmld,{})
但是我有一个
NoReverseMatch
异常。但是当我这样做:def index(request):
return redirect(abc /)
然后它突然起作用。
我不能完全理解为什么。为什么
反向
失败与abc
但不与abc /
?而反向
如何知道重定向应该包括monitor /
?如果我在主要的urls.py中另外有一个名为xyz
的应用程序,还有一个abc
视图? >
解决方案
为什么反向失败与abc但不是与abc /?
因为它将其解释为视图名称(而且您确实有一个名为
'abc' code>,看到你的
monitor.urls
文件)。这意味着Django会调用reverse
来计算URL。值abc /
被解释为实际的URL,这意味着Django不会调用reverse
来确定URL。 / p>
这也解释了为什么
reverse
failed:the name with nameabc
还需要一个名为id
的参数。否则Django将无法查找URL,因为没有没有参数的abc
视图。
基于在文档中,您应该能够使用:
redirect(abc,id = ...)
pre>
其中
...
是id
参数
反向知道重定向应该包括监视器/?
这是因为它知道什么URL可用,1)它知道视图被称为
abc
被定义和2 )它知道monitors.urls
包含在前面的monitor /
。
如果我在主要的urls.py中有另一个名为xyz的应用程序,它还有一个abc视图怎么办?
在这种情况下y ou必须使用命名空间。
I have a couple problems understanding how
redirect
or ratherreverse
really work.In the main urls.py I have:
from django.conf.urls import patterns, include, url from django.views.generic.simple import redirect_to urlpatterns = patterns('', url(r'^$', redirect_to, {'url': '/monitor/'}), url(r'^monitor/', include('monitor.urls')), )
and in
monitors.urls
I have:from django.conf.urls import patterns, include, url urlpatterns = patterns('monitor.views', (r'^$', 'index'), (r'^abc/(?P<id>.*$)', 'abc'), )
When you call
/monitor
I want to redirect it to/monitor/abc
so I did:def index(request): return redirect("abc") def abc(render, id=None): return render_to_response("monitor/list.htmld", {})
But I got an
NoReverseMatch
exception. But when I do:def index(request): return redirect("abc/")
then it suddenly works.
I cannot fully understand why. Why did
reverse
fail withabc
but not withabc/
? And how doesreverse
know that the redirect should includemonitor/
as well? What if I had in the main urls.py another app calledxyz
which also has aabc
view?解决方案Why did reverse fail with 'abc' but not with 'abc/'?
Because it interpreted it as a view name (and you indeed have a view named
'abc'
, see yourmonitor.urls
file). This means Django will callreverse
to compute the URL. The valueabc/
is interpreted as an actual URL which means Django won't callreverse
to determine the URL.This also explains why
reverse
failed: the view with nameabc
also requires an argument calledid
. Otherwise Django won't be able to lookup the URL as there is no view calledabc
without parameters.Based on the documentation you should be able to reverse the URL using:
redirect("abc", id=...)
where
...
is the value of theid
parameter.And how does reverse know that the redirect should include monitor/ as well?
That is because it knows what URLs are available and 1) it knows where the view called
abc
is defined and 2) it knows thatmonitors.urls
is included withmonitor/
in front.What if I had in the main urls.py another app called "xyz" which also has a "abc" view?
In that case you have to use namespaces.
这篇关于了解django.shortcuts.redirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!