2

This serves the media files correctly:

urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This doesn't serve the media files:

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

urlpatterns = [ ... ] 

if settings.DEBUG:
    urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The documentation writes "This is not suitable for production use!", so that is why I need a way to check for DEBUG before serving media files. How can I do that. Why does this approach doesn't work?

1
  • 1
    development server is running in one or two threads, so it's thruput is very limited, so you request are served one-by-one - slowly, plus each static file is being served using basic open-read-send-close operations, without any os advantages (e.g. sendfile, zero-copy) - check uwsgi - it is quite robust and works well with django Commented Jul 14, 2016 at 16:17

1 Answer 1

8

Use

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

(notice '=' after '+' - in your version you are adding static() patterns but not assigning the result to the urlpatterns variable)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.