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 manage.py runserver so Django makes the development environment faster automatically by serving the static files for us.

python manage.py 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 settings.py

  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!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay