Using Whitenoise to Serve Static Files in a Django Production Environment

Posted by

Serving Static Files in Production with whitenoise in Django

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.

0 0 votes
Article Rating
12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@imRJD14
6 months ago

Thanks a lot man
t really helped

@gbaderotoluwalope2910
6 months ago

God will bless you oooo
This sorted me out too. Thanks for putting the video up,
Charge your system next time😉😉😉

@madusan1
6 months ago

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

@jaganivasanth5971
6 months ago

why we need to add under SecurityMiddleware is there any particular reason

@opeyemiatoyebi2212
6 months ago

Thanks a lot

@jahanasultan
6 months ago

Simple and helpful, thank you.

@SuperRevolver22
6 months ago

i followed your steps but my css and javascript are still not loading. Im using render cloud to serve my application. Kindly assist

@abyssinia_entertainment
6 months ago

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.

@nwabuezefranklin7676
6 months ago

Great video, Subcribed.

@mohamedgadelrab5202
6 months ago

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.

@theduzaum9774
6 months ago

you saved me, thanks a lot

@eduardolz12
6 months ago

It helped, thanks