简体   繁体   English

重定向到相对的Django路径

[英]Redirect to relative django path

See these examples: 请参阅以下示例:

https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#examples https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#examples

def my_view(request):
    ...
    return redirect('some-view-name', foo='bar') #view name with optional arguments

def my_view(request):
    ...
    return redirect('/some/url/')

There is no identification to tell Django if the first argument is a view or a URL. 没有标识可以告诉Django第一个参数是视图还是URL。 I wanted to specify 'login', a relative path, but it looks for the view. 我想指定“登录”,一个相对路径,但它会查找视图。 On my local host, I had it working with '/login' since I was working in the root directory, but now after I deployed my project to my web server, I put it in a folder. 在本地主机上,因为我在根目录中工作,所以它可以与“ / login”一起使用,但是现在将项目部署到Web服务器之后,我将其放在文件夹中。 Is specifying '/myfolder/login' (absolute path) the only way to use the redirect shortcut? 指定“ / myfolder / login”(绝对路径)是使用重定向快捷方式的唯一方法吗?

try: 尝试:

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

...

return HttpResponseRedirect( reverse('login', kwargs={'foo': 'bar'}) )

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

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