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?

Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
  • 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 – Jerzyk Jul 14 '16 at 16:17

1 Answers1

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)

rafalmp
  • 3,988
  • 3
  • 28
  • 34