问题描述
我阅读了关于在Django提供静态媒体的发展。
I read this guide about serving static media with Django during development.
我注意到, MEDIA_URL
和 MEDIA_ROOT
不用于此。为什么?有什么区别?
I noticed that MEDIA_URL
and MEDIA_ROOT
were not used in this. Why? What's the difference?
我尝试使用 MEDIA_URL
和 MEDIA_ROOT
,并得到奇怪的结果。
I tried doing it with MEDIA_URL
and MEDIA_ROOT
, and got weird results.
推荐答案
在生产情况下,您希望您的媒体从您的前端Web服务器(Apache,Nginx等),以避免Django / Python进程的额外负载。通常使用MEDIA_URL和MEDIA_ROOT。
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.
运行内置的开发服务器,您需要在url.py文件中设置正确的url - 我通常会使用某些东西像这样:
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<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
从您的设置文件中选择MEDIA_ROOT,这意味着它可以用于开发和生活。
Which picks up the MEDIA_ROOT from your settings file meaning that it works for development and live.
这篇关于在Django开发期间提供静态媒体:为什么不是MEDIA_ROOT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!