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
Set an url prefix if Django is not mounted under / in production #567
Comments
Hi, since Django does not know about this, you need to set 'SERVERS': [{'url': 'https://example.com/backend'}] |
Ok, thanks for the anwser, I tried: |
I achieved this automatically by adding a post-processing hook: from urllib.parse import urljoin
def set_request_server(result, generator, request, public):
if request:
url = urljoin(request.path, ".").rstrip("/")
url = request.build_absolute_uri(url)
result.setdefault("servers", []).append({"url": url})
return result This example works where my schema is hosted at, e.g. |
I dont understand what the preprocessing hook does yet. |
@Wissperwind, sry I made a mistake there. its a list and not just one dict 'SERVERS': [
{
"url": "https://gigantic-server.com/v1",
"description": "Production server"
},
{
"url": "https://development.gigantic-server.com/v1",
"description": "Development server"
}
], this will not add the |
Ah, ok. Now something works.
And it adds the prefix to each call and uses still automatic the current host. But there is still a problem. The prefix is not displayed to each URL. So if I show the swagger page to the developers, they will see /api/login and think, OK, let's call localhost:8000/api/login. But that will not work. They have to call localhost:8000/backend/api/login. Sure I can to explain it to them. But some will forget it sometimes. How about a real prefix setting? |
added new setting of course this is hard-coded in comparison to @ngnpope's hook but i suppose this covers the most typical use-cases |
That works perfectly! |
Ok, so can this issue be closed now? |
yes its released in 0.20.2. it can be closed. |
Hi Nick, where would one place this post-processing hook? |
it does not matter where. you just need to set in the import path in the spectacular setting see doc for more details: https://drf-spectacular.readthedocs.io/en/latest/customization.html#step-6-postprocessing-hooks |
Hi,
The plugin identifies all urls correctly from djangos point of view.
But in production django has to be mounted in to a webserver. And under / is usually the folder for static files for the website.
So I mounted django under /backend. So an endpoint that is /getInfo in django is now /backend/getInfo in reality.
Is there an option to tell this to drf spectacular?
If not I would like to request this feature.
Thanks!
The text was updated successfully, but these errors were encountered: