Code example for production(?) - Serving static files during develpoment

I am currently on the way to deploy my first Django app.
Studying the docs I’ve found How to manage static files.
Within that how-to there is the section Serving static files during development.

I wonder whether the code example within that section is meant for development (as expected from the heading) or rather meant for production (as expected from the description within that section)?

After describing how it works with the default configuration, they say that the default configurations are not suitable for use in production.
But then there follows a code example, which looks similar to other resources where people prepare production environments:

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)

I am a bit confused, because static files normally work by default anyway.
Therefore: Why would that code example describe code for use in the development environment?

What makes you think the description is meant for production?

There’s a different section for deployment further down, but that’s a different paragraph than the one you’re referencing here.

Also, read the text above that example carefully. It explains the situation where that setting is necessary in a development environment and goes on to explain:

This is not suitable for production use! For some common deployment strategies, see How to deploy static files.

One of the key concepts with what I refer to as a “production quality” Django deployment is that Django never serves static files. That functionality is always performed elsewhere - apache, nginx, haproxy, AWS S3, CDN, take your pick. But in production, you’re really better off off-loading that process to something other than your Django application.

1 Like

That’s what makes me wonder:

But I rather thought that this part is really meant for development - as you say.

They don’t “work by default”. They work because of an installed app - staticfiles.

From the docs you referenced:

If you don’t have django.contrib.staticfiles in INSTALLED_APPS , you can still manually serve static files using the django.views.static.serve() view.

That code example goes on to demonstrate how, in development, you can serve static files without the staticfiles app installed.

1 Like

Thank you again.
I didn’t completely realize that this behavior depends on the staticfiles app👍