Serving Static Files in Production with whitenoise in Django
When building a Django application, it’s important to serve static files such as CSS, JavaScript, and images in a production environment. This ensures that your website or web application looks and functions as intended for your users.
One way to serve static files in production with Django is by using whitenoise. Whitenoise is a simple and efficient way to serve static files directly from your Django application without the need for a separate web server or CDN.
Setting up whitenoise
To use whitenoise in your Django application, start by installing the whitenoise package using pip:
pip install whitenoise
Next, add ‘whitenoise.middleware.WhiteNoiseMiddleware’ to the MIDDLEWARE setting in your Django settings file:
MIDDLEWARE = [
# ...
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
Finally, set the STATICFILES_STORAGE setting to ‘whitenoise.storage.CompressedStaticFilesStorage’:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
Collecting static files
Once whitenoise is set up, you can collect all of your static files into a single directory using the collectstatic management command:
python manage.py collectstatic
This command will collect all of the static files from your Django project and any installed apps and place them in the directory specified by the STATIC_ROOT setting.
Configuring your web server
Now that your static files are being served by whitenoise, you can configure your web server to serve the static files directly from the STATIC_ROOT directory. If you’re using a service like Heroku or AWS Elastic Beanstalk, you can simply enable whitenoise and your static files will be served automatically.
Serving static files in a production environment is an important step in ensuring that your website or web application looks and functions as intended for your users. With whitenoise, serving static files in Django is simple and efficient, allowing you to focus on building a great user experience.
Thanks a lot man
t really helped
God will bless you oooo
This sorted me out too. Thanks for putting the video up,
Charge your system next time😉😉😉
You could have just used the BASE_DIR / 'static' instead of using the os.path dec for the path in staticfiles_dirs as you did with the static_root
why we need to add under SecurityMiddleware is there any particular reason
Thanks a lot
Simple and helpful, thank you.
i followed your steps but my css and javascript are still not loading. Im using render cloud to serve my application. Kindly assist
how to fix internal server Error in Django sub route like home/, about/. when I am try to host in server but after installation the sub route not work. The error is 500 Internal Server Error.
Great video, Subcribed.
I had a group demonstration coming and was working 4 straight hours to get Heroku to host static files. Your video helped me completely solve the problem in just 6 minutes. Thank you so much.
you saved me, thanks a lot
It helped, thanks