Serving static media during Django development: Why not MEDIA_ROOT?

前端 未结 3 1338
不知归路
不知归路 2020-12-08 15:35

I read this guide about serving static media with Django during development.

I noticed that MEDIA_URL and MEDIA_ROOT were not used in this.

3条回答
  •  旧巷少年郎
    2020-12-08 15:54

    In a production situation you will want your media to be served from your front end web server (Apache, Nginx or the like) to avoid extra load on the Django/Python process. The MEDIA_URL and MEDIA_ROOT are usually used for this.

    Running the built in Development server you will need to set the correct url in your url.py file - I normally use something like this:

    from django.conf import settings
    
    urlpatterns += patterns('',
        (r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    )
    

    Which picks up the MEDIA_ROOT from your settings file meaning that it works for development and live.

提交回复
热议问题