Skip to content

Use WSGI SCRIPT_NAME as path prefix if defined #1449

@kmohrf

Description

@kmohrf

Describe the bug

The application server (like Gunicorn) or reverse proxy may set SCRIPT_NAME, which is an WSGI environment variable that can be used to serve an application under a path prefix. This has a similar purpose and is related to, but should not be confused with Django’s own FORCE_SCRIPT_NAME setting.

When generating URLs the content of SCRIPT_NAME should be used as a prefix for the generated pathname. Django does this by default now. However, paths in schema generated by drf-spectacular’s do not have this prefix. Instead they are generated as if SCRIPT_NAME were an empty string.

To Reproduce

I’ve created a small repo with minimal configuration to demonstrate the issue:
https://github.com/kmohrf/django_script_name_test

The schema UI under /my-prefix/api/schema/ui/ is actually a nice demonstration because the schema URL directly below the header has the prefix whereas all the paths in the documentation below it don’t have it.

Expected behavior

If SCRIPT_NAME is a non-empty string, all paths in the schema should be prefixed with the content of SCRIPT_NAME.

Thank you for all your work on drf-spectacular and for your time investigating this issue.
Please let me know if I can provide any additional information!

Cheers

Konrad

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions