question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WHITENOISE_STATIC_PREFIX still required if django app deployed on subpath

See original GitHub issue

The docs for WHITENOISE_STATIC_PREFIX currently say

Default: Path component of settings.STATIC_URL (with settings.FORCE_SCRIPT_NAME removed if set) … If your application is not running at the root of the domain and FORCE_SCRIPT_NAME is set then this value will be removed from the STATIC_URL path first to give the correct prefix.

I understand this to mean if in my settings.py I have

FORCE_SCRIPT_NAME = "/dev"
STATIC_URL = "{}/static/".format(FORCE_SCRIPT_NAME)

WHITENOISE_STATIC_PREFIX will automatically be /static/

However, this is not the case and i still need to explicitly set in my settings.py

WHITENOISE_STATIC_PREFIX = "/static/"

otherwise static files will not work.

Is this a bug, or am I misunderstanding something in the docs? I did see https://github.com/evansd/whitenoise/issues/164 which seems similar but different enough (no mention of FORCE_SCRIPT_NAME) where it explicitly mentions needing to do this, but that issue is from 2017 and seems to contradict the above documentation.

am using

  • whitenoise 5.2.0
  • zappa 0.52.0
  • django 3.1.4

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
stewartadamcommented, Sep 26, 2022

I believe this is still an issue, I’ve been experiencing incorrect pathing from static assets with whitenoise which I spent a bit of time debugging in https://github.com/TandoorRecipes/recipes/issues/1878 and https://code.djangoproject.com/ticket/34028.

The root cause seems to be that recent Django versions cache the prefix and setting values based on their first access. Normally first access is a HTTP request so SCRIPT_NAME would be set on it and all is well… However, whitenoise attempts to access the configured STATIC_URL during middleware initialization, as you can see here:

recipes-web_recipes-1  |   File "/opt/recipes/recipes/wsgi.py", line 15, in <module>
recipes-web_recipes-1  |     _application = get_wsgi_application()
recipes-web_recipes-1  |   File "/opt/recipes/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
recipes-web_recipes-1  |     return WSGIHandler()
recipes-web_recipes-1  |   File "/opt/recipes/venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 126, in __init__
recipes-web_recipes-1  |     self.load_middleware()
recipes-web_recipes-1  |   File "/opt/recipes/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 61, in load_middleware
recipes-web_recipes-1  |     mw_instance = middleware(adapted_handler)
recipes-web_recipes-1  |   File "/opt/recipes/venv/lib/python3.10/site-packages/whitenoise/middleware.py", line 47, in __init__
recipes-web_recipes-1  |     self.configure_from_settings(settings)
recipes-web_recipes-1  |   File "/opt/recipes/venv/lib/python3.10/site-packages/whitenoise/middleware.py", line 86, in configure_from_settings
recipes-web_recipes-1  |     self.static_prefix = urlparse(settings.STATIC_URL or "").path
recipes-web_recipes-1  |   File "/opt/recipes/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 93, in __getattr__
recipes-web_recipes-1  |     val = self._add_script_prefix(val)
recipes-web_recipes-1  |   File "/opt/recipes/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 144, in _add_script_prefix
recipes-web_recipes-1  |     traceback.print_stack()

Since this is during initialization and outside a HTTP request context, SCRIPT_NAME is absent and therefore the Django prefix gets incorrectly returned as ‘/’ (i.e. STATIC_URL gets set to ‘/static/’ and then remains that way). This causes all the asset URIs to be wrong when a real request comes in with SCRIPT_NAME header set.

In theory setting FORCE_SCRIPT_NAME should address this by short-circuiting SCRIPT_NAME and ensuring the Django prefix is set correct on whitenoise middleware initialization, but in my testing I didn’t see that behavior. The prefix only gets set on the first HTTP request, so until then even with FORCE_SCRIPT_NAME set the prefix is still ‘/’ according to Django - and that’s what whitenoise sees. Note sure if thats a Django issue or if whitenoise needs to add an explicit check for FORCE_SCRIPT_NAME.

0reactions
bluespider42commented, Jul 22, 2021

I had a similar issue which I solved by setting WHITENOISE_STATIC_PREFIX = "/static/" although I’m not sure why this worked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WHITENOISE_STATIC_PREFIX still required if django app ...
WHITENOISE_STATIC_PREFIX still required if django app deployed on subpath ... If your application is not running at the root of the domain and ......
Read more >
Django in subdirectory - admin site is not working
Within that server I have a location setup to proxy onto an Nginx service + Gunicorn for each sub-project. The nginx default location ......
Read more >
How to manage static files (e.g. images, JavaScript, CSS)
We need to be able to point Django at the right one, and the best way to ensure this is by namespacing them....
Read more >
Securely Deploy a Django App With Gunicorn, Nginx, & HTTPS
If you want your site to be accessible at a real-looking URL, you'll need to claim a domain name and tie it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found