简体   繁体   English

Django重定向网址

[英]Django Redirect URL

I am new to Django, and i am now trying to use the HttpResponseRedirect() function. 我是Django的新手,现在尝试使用HttpResponseRedirect()函数。 But I am so confused that if I use it like HttpResponseRedirect('good/') , and the current page is '/bad/' , it can only be redirected to '/bad/good/' , which is an url of current page appended with the url value from the HttpResponseRedirect() function. 但是我很困惑,如果我像HttpResponseRedirect('good/')这样使用它,并且当前页面是'/bad/' ,则只能将其重定向到'/bad/good/' ,这是当前URL页面附加了HttpResponseRedirect()函数的url值。 I tried to search google, and could not find any solution. 我试图搜索google,但找不到任何解决方案。

How can I redirect to the page with specific url? 如何重定向到具有特定网址的页面? For example, HttpResponseRedirect('/good/') to /good/ rather than /bad/good/ ? 例如, HttpResponseRedirect('/good/')/good/而不是/bad/good/吗?

Surely you must see that there's a difference between 'good/' and '/good/' ? 当然您必须看到'good/''/good/'之间有区别吗? The former will always add itself onto the existing page, whereas the latter will start from the root. 前者将始终将其自身添加到现有页面上,而后者将从根目录开始。 This is basic web behaviour, and nothing to do with Django. 这是基本的网络行为,与Django无关。

In any case, you should never hard-code URLs like that, but should use Django's URL-reversing functionality to calculate the URLs dynamically. 在任何情况下,您都不应这样对URL进行硬编码,而应使用Django的URL反向功能动态地计算URL。

If you want to redirect to urls in your domain, you can use redirect . 如果您想重定向到您域中的网址,则可以使用redirect You can use redirect to view with by using patterns in your urls.py file or to any urls you 'd like. 您可以通过在urls.py文件或您想要的任何url中使用模式来使用重定向进行查看。 Although I strongly encourage the usage of views as indicates the different tutorials available on their website. 尽管我强烈建议您使用视图,因为它们表示可以在其网站上找到不同的教程。

Better check out django documentation that is one of the most complete out there (to my humble opinion) 最好查看django文档,它是其中最完整的文档(据我的拙见)

/edit for lack of clarity indeed. / edit确实不够清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM