Docker部署Django网站静态文件404错误求助
Alright, let's work through this static resource issue—it’s a common pitfall when deploying Django behind a URL prefix, especially in Docker. Here’s how to resolve it step by step:
1. Update STATIC_URL to Include Your URL Prefix
Since your server adds a URL prefix like /Something/, your current STATIC_URL = '/static/' generates paths that don’t account for this prefix. For example, templates are trying to load http://127.0.0.1:8000/static/css/style.css instead of the correct http://127.0.0.1:8000/Something/static/css/style.css.
Update your settings.py:
# Replace 'Something' with your actual URL prefix STATIC_URL = '/Something/static/'
2. Configure STATIC_ROOT and Run collectstatic
In production environments (like Docker), Django doesn’t automatically serve static files from STATICFILES_DIRS—you need to collect all static assets into a single directory using collectstatic.
First, add STATIC_ROOT to settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Then, ensure this command runs in your Docker setup:
- In your Dockerfile: Add this line before starting the server (to collect static files during image build):
RUN python manage.py collectstatic --noinput - Or in docker-compose.yml: Add it as part of the startup command:
services: web: build: . command: > sh -c "python manage.py collectstatic --noinput && python manage.py runserver 0.0.0.0:8000" # ... other configs like ports, volumes
3. Verify Template Static References
Double-check that you’re using Django’s static template tag to load assets, not hardcoding paths. Hardcoded paths won’t pick up the updated STATIC_URL with your prefix.
❌ Bad (hardcoded):
<link rel="stylesheet" href="/static/css/style.css">
✅ Good (template tag):
{% load static %} <link rel="stylesheet" href="{% static 'css/style.css' %}">
4. Align URL Prefix Configuration
If you used FORCE_SCRIPT_NAME to set the URL prefix in settings.py, ensure it’s correctly set:
FORCE_SCRIPT_NAME = '/Something'
This tells Django to prepend this prefix to all generated URLs (including static assets).
Alternatively, if you added the prefix directly in your root urls.py, make sure static URL patterns are included correctly:
from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('Something/', include('your_app.urls')), # ... other paths ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
5. (Optional) Use Nginx to Serve Static Files
For production, it’s best practice to let Nginx (or another web server) serve static files instead of Django. If you’re using Nginx as a reverse proxy in Docker, add a location block to handle static requests:
server { # ... other configs like proxy_pass to Django location /Something/static/ { alias /path/to/your/app/staticfiles/; # Match STATIC_ROOT in settings.py expires 30d; access_log off; } }
Make sure the alias path matches the STATIC_ROOT directory in your Django container, and that the volume is mounted correctly in docker-compose if needed.
内容的提问来源于stack exchange,提问作者idature

