Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning redirect responses in endpoints #224

Closed
itaisteinherz opened this issue Sep 10, 2021 · 9 comments
Closed

Returning redirect responses in endpoints #224

itaisteinherz opened this issue Sep 10, 2021 · 9 comments

Comments

@itaisteinherz
Copy link

I recently started using Django Ninja, and encountered a scenario where I need to return a redirect response to another endpoint (though it would be cool if I could redirect to any URL). I couldn't find a way to do this, and was unable to use something like redirect()/HttpResponseRedirect (the error I received was Pydantic-related, but I assume that even if validation was disabled, this still wouldn't have worked).

Is there a way to do this in Django Ninja?

@itaisteinherz itaisteinherz changed the title Returning redirect response in endpoint Returning redirect responses in endpoints Sep 10, 2021
@vitalik
Copy link
Owner

vitalik commented Sep 11, 2021

Hi @itaisteinherz
Redirects should works just fine..

could you share snippet of code that fails for you ?

@itaisteinherz
Copy link
Author

@vitalik Thanks for the quick reply!

This is a simplified version of what I tried:

from ninja import NinjaAPI

api = NinjaAPI()


@api.get('/something')
def some_redirect():
    return 302, '/another-endpoint'

# The API is then used in urls.py

@vitalik
Copy link
Owner

vitalik commented Sep 11, 2021

@itaisteinherz
well seems you forgot the first parameter request

this works for me :

from django.shortcuts import redirect

api = NinjaAPI()


@api.get("/something")
def some_redirect(request): # < request
    return redirect("/some-path")

test:

$ http GET 'http://localhost:8000/api/something'
HTTP/1.1 302 Found
Content-Length: 0
Content-Type: text/html; charset=utf-8
Date: Sat, 11 Sep 2021 13:52:12 GMT
Location: /some-path
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.6.9
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

@itaisteinherz
Copy link
Author

itaisteinherz commented Sep 11, 2021

I forgot to add the request argument in my comment, however I did add it in my actual codebase.

This probably didn't work for me because I did return 302, redirect("/some-path") in my endpoint, which I expected to work, but results in this error:

Schema for status 302 is not set in response dict_keys([200])
Traceback (most recent call last):
  File "C:\Users\itais\AppData\Local\Programs\Python\Python39\lib\site-packages\ninja\operation.py", line 95, in run
    return self._result_to_response(request, result)
  File "C:\Users\itais\AppData\Local\Programs\Python\Python39\lib\site-packages\ninja\operation.py", line 174, in _result_to_response
    raise ConfigError(
ninja.errors.ConfigError: Schema for status 302 is not set in response dict_keys([200])
Internal Server Error: /something
[11/Sep/2021 17:06:21] "GET /something HTTP/1.1" 500 450

In my opinion this is not trivial. Also, I'd like to have a way to control the specific status code used in the redirect.

  1. Would you be in favor of documenting this in the docs?
  2. Is there a way to use a specific status code for the redirect?

@stephenrauch
Copy link
Contributor

The redirect() function returns a HttpResponse Object which ninja passes through unmolested. If the redirect() shortcut does not meet your needs you can construct any Django response object, with any status code or other details that you need, and return it from your view function.

@vitalik
Copy link
Owner

vitalik commented Sep 11, 2021

@itaisteinherz
if you returning redirect - you do not need to output status code

change this:

return 302, redirect("/some-path")

to this:

return redirect("/some-path")

@itaisteinherz
Copy link
Author

Got it.

I would add to the docs that you can return Django response objects from Ninja endpoints, and include redirects as an example (I went over the whole docs a few days ago and don't remember seeing this, and think that this is worth adding). I'd be happy to contribute the change myself if you're interested.

Thanks for explaining @vitalik @stephenrauch!

@thkwon1
Copy link

thkwon1 commented Dec 27, 2022

@itaisteinherz

Hello, Thanks to your question, I could solve my problem about redirect that is same your problem. Thank you 😄

In last comment, you said that you will contribute docs about redirect but, I can not find any redirect part in docs 😢
If you will have a plan that contributing docs about this issue, please contribute 🙏🏿

@vitalik
Copy link
Owner

vitalik commented Dec 29, 2022

done 39a1e5b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants