Menu Close

How to add Python Django MEDIA_URL and MEDIA_ROOT?

To add Python Django MEDIA_URL and MEDIA_ROOT, we add them to our URL config to serve uploaded files when developing our app locally.

For instance, we write

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

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

to call static with settings.MEDIA_URL and document_root=settings.MEDIA_ROOT to add the route to expose the uploaded files when developing our app locally.

Posted in Python, Python Answers