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
Comments
Hi @itaisteinherz could you share snippet of code that fails for you ? |
@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 |
@itaisteinherz this works for me : from django.shortcuts import redirect
api = NinjaAPI()
@api.get("/something")
def some_redirect(request): # < request
return redirect("/some-path") test:
|
I forgot to add the This probably didn't work for me because I did
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.
|
The |
@itaisteinherz change this: return 302, redirect("/some-path") to this: return redirect("/some-path") |
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! |
Hello, Thanks to your question, I could solve my problem about In last comment, you said that you will contribute docs about |
done 39a1e5b |
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?
The text was updated successfully, but these errors were encountered: