了解django.shortcuts.redirect [英] Understanding django.shortcuts.redirect

查看:114
本文介绍了了解django.shortcuts.redirect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题,了解重定向或者相反反向真的有效。



在主要的urls.py中我有:

 从django.conf.urls导入模式, include,url 
from django.views.generic.simple import redirect_to

urlpatterns = patterns('',
url(r'^ $',redirect_to,{'url' :'/ monitor /'}),
url(r'^ monitor /',include('monitor.urls')),

pre>

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 name abc 还需要一个名为 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 rather reverse 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 with abc but not with abc/? And how does reverse know that the redirect should include monitor/ as well? What if I had in the main urls.py another app called xyz which also has a abc 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 your monitor.urls file). This means Django will call reverse to compute the URL. The value abc/ is interpreted as an actual URL which means Django won't call reverse to determine the URL.

This also explains why reverse failed: the view with name abc also requires an argument called id. Otherwise Django won't be able to lookup the URL as there is no view called abc without parameters.

Based on the documentation you should be able to reverse the URL using:

redirect("abc", id=...)

where ... is the value of the id 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 that monitors.urls is included with monitor/ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆