简体   繁体   English

Django重定向到自定义URL

[英]Django redirect to custom URL

From my Django app, how to I redirect a user to somescheme://someurl.com? 从我的Django应用程序,如何将用户重定向到somescheme://someurl.com?

To give you some context in case it helps, I have a working oauth2 server written in Python/Django and I need to allow users to register redirect_uris that have a custom URL scheme. 为了给你一些上下文以防万一,我有一个用Python / Django编写的工作oauth2服务器,我需要允许用户注册具有自定义URL方案的redirect_uris。 This custom URL scheme is used for handling the redirect within native apps . 此自定义URL方案用于处理本机应用程序中的重定向

My first reaction was to use an HttpResponseRedirect, but this URL has a custom scheme and isn't HTTP so I'm guessing this is not what I want. 我的第一反应是使用HttpResponseRedirect,但是这个URL有一个自定义方案而且不是HTTP所以我猜这不是我想要的。 Thanks in advance for any advice you can give. 提前感谢您提出的任何建议。

Edit: I did try this and Django returns the response redirect correctly without throwing an error, but the browser does not redirect to this URL. 编辑:我确实尝试了这一点,Django正确返回响应重定向而不会抛出错误,但浏览器不会重定向到此URL。 I'm using Chrome to test this. 我正在使用Chrome来测试这个。

Edit 2: HttpResponseRedirect works fine in safari. 编辑2:HttpResponseRedirect在safari中工作正常。

This actually should not work as Django is only allowing redirects to http , https and ftp by default for security reasons: 这实际上不应该工作,因为出于安全原因,Django默认只允许重定向到httphttpsftp

https://www.djangoproject.com/weblog/2012/jul/30/security-releases-issued/ https://www.djangoproject.com/weblog/2012/jul/30/security-releases-issued/

I was having the same issue with OAuth and redirect to custom schemes. 我遇到了与OAuth相同的问题并重定向到自定义方案。
Django (on Apache) is throwing 500's ( django.core.exceptions.SuspiciousOperation ) when redirecting to custom schemes. Django(在Apache上)在重定向到自定义方案时抛出500( django.core.exceptions.SuspiciousOperation )。 The solution is to create your own HttpResponseRedirect subclass or just do: 解决方案是创建自己的HttpResponseRedirect子类,或者只做:

location = < your redirect URL >
res = HttpResponse(location, status=302)
res['Location'] = location
return res

class HttpResponseRedirect class HttpResponseRedirect

The first argument to the constructor is required -- the path to redirect to. 构造函数的第一个参数是必需的 - 重定向到的路径。 This can >be a fully qualified URL (eg 'http://www.yahoo.com/search/') or an absolute path with no >domain (eg '/search/'). 这可以是一个完全限定的URL(例如'http://www.yahoo.com/search/')或没有>域的绝对路径(例如'/ search /')。 See HttpResponse for other optional constructor arguments. 有关其他可选构造函数参数,请参阅HttpResponse。 Note >that this returns an HTTP status code 302. 注意>这将返回HTTP状态代码302。

This is from here: https://docs.djangoproject.com/en/dev/ref/request-response/ 这是从这里: https//docs.djangoproject.com/en/dev/ref/request-response/

It should work anyway from what I'm reading. 无论如何它应该从我正在阅读的内容中发挥作用。

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

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