Django redirect to custom URL

2024/11/17 14:31:08

From my Django app, how to I redirect a user to 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. This custom URL scheme is used for handling the redirect within native apps.

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. 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. I'm using Chrome to test this.

Edit 2: HttpResponseRedirect works fine in safari.

Answer

This actually should not work as Django is only allowing redirects to http, https and ftp by default for security reasons:

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

I was having the same issue with OAuth and redirect to custom schemes.
Django (on Apache) is throwing 500's (django.core.exceptions.SuspiciousOperation) when redirecting to custom schemes. The solution is to create your own HttpResponseRedirect subclass or just do:

location = < your redirect URL >
res = HttpResponse(location, status=302)
res['Location'] = location
return res
https://en.xdnf.cn/q/71179.html

Related Q&A

Python embedding with threads -- avoiding deadlocks?

Is there any way to embed python, allow callbacks from python to C++, allowing the Pythhon code to spawn threads, and avoiding deadlocks?The problem is this:To call into Python, I need to hold the GIL…

RuntimeError: Event loop is closed when using pytest-asyncio to test FastAPI routes

I received the errorRuntimeError: Event loop is closedeach time I try to make more than one async call inside my test. I already tried to use all other suggestions from other Stack Overflow posts to re…

Adjust threshold cros_val_score sklearn

There is a way to set the threshold cross_val_score sklearn?Ive trained a model, then I adjust the threshold to 0.22. The model in the following below :# Try with Threshold pred_proba = LGBM_Model.pre…

Efficiently insert multiple elements in a list (or another data structure) keeping their order

I have a list of items that should be inserted in a list-like data structure one after the other, and I have the indexes at which each item should be inserted. For example: items = [itemX, itemY, itemZ…

matplotlib versions =3 does not include a find()

I am running a very simple Python script: from tftb.generators import amgauss, fmlinI get this error: C:\Users\Anaconda3\envs\tf_gpu\lib\site-packages\tftb-0.0.1-py3.6.egg\tftb\processing\affine.py in …

How to fix Field defines a relation with the model auth.User, which has been swapped out

I am trying to change my user model to a custom one. I do not mind dropping my database and just using a new one, but when I try it to run makemigrations i get this errorbookings.Session.session_client…

Generate and parse Python code from C# application

I need to generate Python code to be more specific IronPyton. I also need to be able to parse the code and to load it into AST. I just started looking at some tools. I played with "Oslo" and …

An efficient way to calculate the mean of each column or row of non-zero elements

I have a numpy array for ratings given by users on movies. The rating is between 1 and 5, while 0 means that a user does not rate on a movie. I want to calculate the average rating of each movie, and t…

Selecting unique observations in a pandas data frame

I have a pandas data frame with a column uniqueid. I would like to remove all duplicates from the data frame based on this column, such that all remaining observations are unique.

GEdit/Python execution plugin?

Im just starting out learning python with GEdit plus various plugins as my IDE.Visual Studio/F# has a feature which permits the highlighting on a piece of text in the code window which then, on a keyp…