30
loading...
This website collects cookies to deliver better user experience
runserver
helpfully handles your static files for you. But the documentation emphatically tells you not to do that in production: "This method is grossly inefficient and probably insecure, so it is unsuitable for production"./var/app/static
(so that's your STATIC_ROOT
in Django settings) and served under /static/
prefix (that's your STATIC_URL
in Django), here's an example Nginx config to serve them:location /static/ {
root /var/app;
}
MEDIA_URL
is /media/
and MEDIA_ROOT
is /var/app/media
, you'd add another block:location /media/ {
root /var/app;
}
STATIC_ROOT
end in STATIC_URL
and MEDIA_ROOT
end in MEDIA_URL
- it makes configuration simpler.DocumentRoot /var/app;
Alias /static /var/app/static;
Alias /media /var/app/media;
<Directory /var/app>
Deny from all
</Directory>
<Directory /var/app/static>
Allow from all
</Directory>
<Directory /var/app/media>
Allow from all
</Directory>
STATICFILES_STORAGE
(for static files) and DEFAULT_FILE_STORAGE
(for media files) to your preferred backend.STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
manage.py collectstatic
as part of your deployment procedure, the storages engine will upload all static files to S3. Media files will be uploaded to S3 when you try to save them server-side.MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
django-storages
and whitenoise
support compression and manifest storage for your content.script.js
might be renamed to script.f9f204dfa.js
, where f9f204dfa
is a manifest, or checksum, of the file contents. The manifest changes each time the file contents change, meaning each change to a file will generate a new, unique, name.{% static %}
template tag. The template tag will rewrite the name to use the actual (current) manifest. If you hardcode path like script.js
in your template, it will not be found.