Django static files include application CSS, JavaScript and administrator assets. Production delivery needs a defined collection and serving process rather than relying on the development server.
IN THIS ARTICLE
What you need before you begin
Identify the production settings module, static storage backend and intended static URL. Confirm the application account can write the collection destination or publish through the configured storage service.
01 Serve Django static assets after deployment
Configure
STATIC_URLand a suitableSTATIC_ROOTfor local collection. Keep source asset directories separate from the final collected directory.Run
python manage.py collectstaticusing the application virtual environment and production settings. Review any missing source files or manifest errors.Configure the approved web server or static storage service to deliver the collected files at the intended URL. Do not expose the entire project to make one asset directory accessible.
Deploy the resulting assets and their manifest together with the release that references them. Use the selected storage backend rules when filenames are hashed.
Apply suitable browser caching to versioned assets and retain files required by already-open pages during a release transition.
02 Example: collect static files without mixing in uploads
For a local static-file deployment, define a collection directory separately from persistent user uploads. These settings assume your project defines BASE_DIR. Replace the shared-storage placeholder with an approved existing directory outside the replaceable release tree.
from pathlib import Path
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static-collected'
MEDIA_URL = '/media/'
MEDIA_ROOT = Path('/home/ACCOUNT/shared/uploads')
Run .venv/bin/python manage.py collectstatic --noinput only after selecting the correct settings and storage backend. Point the approved static-file serving mechanism at STATIC_ROOT. Arrange ownership, access and backups for the external upload directory separately; a path setting does not create it or configure safe media delivery.
Expected result: The administrator login loads its actual stylesheet with the correct content type, and a representative customer image remains available after the release changes. A missing asset returns a real missing-file response rather than the application’s HTML fallback.
Confirm the expected result
Open the Django administrator login and a customer page. Check that CSS and JavaScript return the expected contents and content types rather than an HTML fallback.
Troubleshoot unexpected results
A 404 after successful collection often means the web server points to another directory or the public URL prefix is wrong. A manifest error needs the release build and referenced filenames checked.
A common question
Can static files and user uploads share one directory?
Keep them separate. Static assets can be rebuilt from the application, while customer uploads are persistent data that must survive collection, deployment and rollback.