pythondjangomedia-url

Why should you not use the MEDIA_ROOT and MEDIA_URL in the production level of Django?


I'm trying to find the answer for the name of the question that I posted, but I couldn't seem to find the answer.

I've been looking at several tutorials in Django and they keep on telling me that you should not use the MEDIA_URL and MEDIA_ROOT type of convention when you're at the production level of Django.

But why is that? Is there a specific reason for it?


Solution

  • There's nothing wrong with those variables in particular. The fact is you never want to serve static files using Django's dev server in production, both because of performance and security issues.

    The dev server is very helpful, because it allows you to use nothing but Django to serve static files and media files during development, but it is not a production web server in any way (such as Nginx for example).

    The docs are, as always, helpful: https://docs.djangoproject.com/en/3.1/howto/static-files/#serving-uploaded-files-in-development

    What applies to static files also applies to media files. For strategies about static files deployment, there's a whole page on the docs too.

    Some packages, such as Whitenoise, help when dealing with static files in production, but Whitenoise can't be used for media (user-uploaded) static files (because roughly it discovers static files at start).