DEV Community

Ritik Soni
Ritik Soni

Posted on

How to Handle Static Files in Production server of Django.

while working in the Development environment we use python runserver so Django makes the development environment faster automatically by serving the static files for us.

python runserver command uses django.contrib.staticfiles module to server the static files in development when DEBUG is set to True. if we set DEBUG to False then it does not uses django.contrib.staticfiles. and then we have to server our static files manually by

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Enter fullscreen mode Exit fullscreen mode

as described in the Docs

In Production Django just dont want to serve static files for us its just neglect all of them.. so for that we have light weight library, which can easily solve this problem, Called whitenoise.

solve your problem in three steps:-

  1. install whitenoise pip install whitenoise.

  2. "whitenoise.middleware.WhiteNoiseMiddleware", add this middleware to your MIDDLEWARE list in

  3. "whitenoise.runserver_nostatic", add this line to your INSTALLED_APPS list.

This is how i handled my static files in production as like so many other guys. Hope you can too!

Top comments (0)