6👍
Django does not support serving static files in production. However, the fantastic WhiteNoise project can integrate into your Django application, and was designed with exactly this purpose in mind.
pip install whitenoise
add whitenoise to your requirements.txt
add this code in app/wsgi.py
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
7👍
I’ve spent some hours before I was able to figure out this. @VipinMohan solution works for whitenoise<4. However, in version 4+, WhiteNoise removes some options which were deprecated in the previous major release. For the record, I am using Django 2.1.
From the docs:
The WhiteNoise middleware should be placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
# the next line of code is the one that solved my problems
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
]
Pay attention to the Note in the section of the provided link.
You might find other third-party middleware that suggests it should be given highest priority at the top of the middleware list. Unless you understand exactly what is happening you should ignore this advice and always place WhiteNoiseMiddleware above other middleware.
- [Django]-Django-allauth returns error "Reverse … with arguments '()' and keyword arguments '{}' not found"
- [Django]-Django on Strato webspace
1👍
Add collectstatic to Procfile
web: python manage.py collectstatic --no-input; gunicorn myapp.wsgi --log-file - --log-level debug
Thanks to this stack overflow answer
- [Django]-How to allow editing of templates for email by admin user in Django?
- [Django]-How do I write an update join postgres query as queryset in Django 1.9?
- [Django]-Edit the opposite side of a many to many relationship with django generic form
1👍
WhiteNoise configuration should be changed if you use v4.0 or later.
Please refer this whitenoise-changelog
- [Django]-Django.db.utils.OperationalError: (1091, "Can't DROP 'company_id'; check that column/key exists")
- [Django]-How to use django migrate without confirmation
- [Django]-Django: how to map the results of a raw sql query to model instances in admin list view?