Monday, October 7, 2019

Whitenoise untuk Static Files Manager di Django

Install Whitenoise

pip install whitenoise whitenoise[brotli]

Setup pada File settings.py

Tambahkan snippet berikut:

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
     # ... 
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

# ... 
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

Jalankan perintah Collectstatic

./manage.py collectstatic

Tambahkan Path di urls.py

Terakhir, jangan lupa untuk menambahkan path staticnya:

# ...
from django.conf import settings 
from django.conf.urls.static import static
# ...
]  + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

No comments:

Post a Comment