简体   繁体   English

Django中的静态文件和媒体文件有什么区别?

[英]What is the difference between static files and media files in Django?

I'm moving to Django 1.3 and find this separation of media and static files a bit confusing. 我正在转向Django 1.3,发现媒体和静态文件的这种分离有点令人困惑。 Here is how default settings.py looks like: 以下是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/'

What should I put into MEDIA_ROOT and a STATIC_ROOT ? 我应该把什么放入MEDIA_ROOTSTATIC_ROOT Should those be separate directories? 那些应该是单独的目录吗? What is the difference? 有什么不同?

静态文件适用于javascript /图像等,但媒体文件适用于用户上传的内容。

As Uku Loskit said, static files are for things like your applications' css files, javascript files, images, etc. Media files are typically user or admin uploadable files. 正如Uku Loskit所说,静态文件适用于应用程序的css文件,javascript文件,图像等。媒体文件通常是用户或管理员可上载的文件。

Normally you will want MEDIA_ROOT and STATIC_ROOT to be separate directories. 通常,您希望MEDIA_ROOTSTATIC_ROOT是单独的目录。 Keep in mind that STATIC_ROOT is where the management command collectstatic will place all the static files it finds. 请记住, STATIC_ROOT是管理命令collectstatic将放置它找到的所有静态文件的位置。 In production, you then configure your webserver to serve the files out of STATIC_ROOT when given a request that starts with STATIC_URL . 在生产中,然后在给定以STATIC_URL开头的请求时,将您的Web服务器配置为从STATIC_ROOT提供文件。 If you are using the Django devserver for development, it will automatically serve static files. 如果您使用Django devserver进行开发,它将自动提供静态文件。

The staticfiles application thus disentangles user uploaded media from application media, thus making deployment, backups, and version control easier. 因此,静态文件应用程序将用户上载的媒体从应用程序媒体中解开,从而使部署,备份和版本控制更容易。 Prior to the staticfiles app, it was common for developers to have the media files mixed in with static application assets. 在staticfiles应用程序之前,开发人员通常将媒体文件与静态应用程序资产混合在一起。

The 1.3 docs for staticfiles have been steadily improving; 静态文件的1.3文档一直在稳步提高; for more details, look at the how-to . 有关详细信息,请查看操作方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM