![](/img/trans.png)
[英]The real difference between MEDIA_ROOT (media files) and STATIC_ROOT (static files) in python django and how to use them correctly
[英]What is the difference between static files and media files in Django?
我正在转向Django 1.3,发现媒体和静态文件的这种分离有点令人困惑。 以下是default settings.py
外观:
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''
# Absolute path to the directory that holds static files.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL that handles the static files served from STATIC_ROOT.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
我应该把什么放入MEDIA_ROOT
和STATIC_ROOT
? 那些应该是单独的目录吗? 有什么不同?
静态文件适用于javascript /图像等,但媒体文件适用于用户上传的内容。
正如Uku Loskit所说,静态文件适用于应用程序的css文件,javascript文件,图像等。媒体文件通常是用户或管理员可上载的文件。
通常,您希望MEDIA_ROOT
和STATIC_ROOT
是单独的目录。 请记住, STATIC_ROOT
是管理命令collectstatic
将放置它找到的所有静态文件的位置。 在生产中,然后在给定以STATIC_URL
开头的请求时,将您的Web服务器配置为从STATIC_ROOT
提供文件。 如果您使用Django devserver进行开发,它将自动提供静态文件。
因此,静态文件应用程序将用户上载的媒体从应用程序媒体中解开,从而使部署,备份和版本控制更容易。 在staticfiles应用程序之前,开发人员通常将媒体文件与静态应用程序资产混合在一起。
静态文件的1.3文档一直在稳步提高; 有关详细信息,请查看操作方法 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.