Python STATICFILES_DIR、STATIC_ROOT 和 MEDIA_ROOT 之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24022558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT
提问by dev-jim
What are the differences of these three static url?
这三个静态url有什么区别?
I am not sure if I am right, I am using the MEDIA_ROOT
to store my uploaded photos (via models.ImageField()
)
我不确定我是否正确,我正在使用MEDIA_ROOT
存储我上传的照片(通过models.ImageField()
)
However, I created a JS script to my admin and in admin.py
. I defined the media as below:
但是,我为管理员创建了一个 JS 脚本,并在admin.py
. 我将媒体定义如下:
....
class Media:
js = ('/admin/custom.js', )
and my settings.py
:
和我的settings.py
:
....
STATIC_ROOT = "/home/user/project/django1/top/listing/static"
and I added the custom.js
to STATIC_ROOT/admin/custom.js
, but it is not working. Throwing 404 not found error.
我添加custom.js
到STATIC_ROOT/admin/custom.js
,但它不起作用。抛出 404 not found 错误。
And then I change the STATIC_ROOT
to STATICFILES_DIRS
, and it works!!
然后我将其更改STATIC_ROOT
为STATICFILES_DIRS
,它起作用了!!
....
STATICFILES_DIRS = "/home/user/project/django1/top/listing/static"
So, I am not understand what is going on here. In fact, I just don't understand what is the difference between STATIC_ROOT
and STATICFILES_DIRS
.
所以,我不明白这里发生了什么。事实上,我只是不明白STATIC_ROOT
和之间有什么区别STATICFILES_DIRS
。
Currently I am testing Django in my machine via virtualenv, not deployed yet, is it the reason STATIC_ROOT
not working??
目前我正在我的机器上通过 virtualenv 测试 Django,还没有部署,这是STATIC_ROOT
不工作的原因吗??
采纳答案by Maxime Lorant
You can find these settings in the Django documentation. Here are my own definitions and quotations from the documentation:
您可以在Django 文档中找到这些设置。以下是我自己在文档中的定义和引用:
MEDIA_ROOT
is the folder where files uploaded usingFileField
will go.Absolute filesystem path to the directory that will hold user-uploaded files.
STATIC_ROOT
is the folder where static files will be stored after usingmanage.py collectstatic
The absolute path to the directory where
collectstatic
will collect static files for deployment.If the
staticfiles
contrib app is enabled (default) thecollectstatic
management command will collect static files into this directory. See the howto on managing static files for more details about usage.STATICFILES_DIRS
is the list of folders where Django will search for additional static files aside from thestatic
folder of each app installed.This setting defines the additional locations the staticfiles app will traverse if the
FileSystemFinder
finder is enabled, e.g. if you use thecollectstatic
orfindstatic
management command or use the static file serving view.
MEDIA_ROOT
是使用上传的文件所在的文件夹FileField
。将保存用户上传文件的目录的绝对文件系统路径。
STATIC_ROOT
是使用后存放静态文件的文件夹manage.py collectstatic
collectstatic
将收集用于部署的静态文件的目录的绝对路径。如果
staticfiles
启用了contrib 应用程序(默认),collectstatic
管理命令会将静态文件收集到此目录中。有关使用的更多详细信息,请参阅管理静态文件的方法。STATICFILES_DIRS
是 Django 将在其中搜索除static
安装的每个应用程序的文件夹之外的其他静态文件的文件夹列表。此设置定义了如果
FileSystemFinder
启用了查找程序,staticfiles 应用程序将遍历的其他位置,例如,如果您使用collectstatic
或findstatic
管理命令或使用静态文件服务视图。
In your settings, you should have:
在您的设置中,您应该:
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# Make a tuple of strings instead of a string
STATICFILES_DIRS = ("/home/user/project/django1/top/listing/static", )
...where:
...在哪里:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
as defined in the default Django settings.py
now.
正如settings.py
现在在默认 Django 中定义的那样。
回答by pygaur
Difference between STATICFILES_DIRS
and STATIC_ROOT
STATICFILES_DIRS
和之间的区别STATIC_ROOT
The STATICFILES_DIRS
can contain other directories (not necessarily app directories) with static files and these static files will be collected into your STATIC_ROOT when you run collectstatic
. These static files will then be served by your web server and they will be served from your STATIC_ROOT.
该STATICFILES_DIRS
可以包含其他目录(不一定是应用程序的目录)与静态文件和这些静态文件,当您运行将被收集到您的STATIC_ROOT collectstatic
。然后这些静态文件将由您的 Web 服务器提供,它们将从您的 STATIC_ROOT 提供。
If you have files currently in your STATIC_ROOT that you wish to serve then you need to move these to a different directory and put that other directory in STATICFILES_DIRS
. Your STATIC_ROOT
directory should be empty and all static files should be collected into that directory.
如果您的 STATIC_ROOT 中当前有您希望提供服务的文件,那么您需要将这些文件移动到其他目录并将该其他目录放入STATICFILES_DIRS
. 您的STATIC_ROOT
目录应该是空的,并且所有静态文件都应该收集到该目录中。
MEDIA_ROOT
where media files ,all uploaded files goes.
Example : Images, Files
MEDIA_ROOT
媒体文件,所有上传的文件在哪里。示例:图像、文件
回答by Max Malysh
Development
发展
STATIC_ROOT
is useless during development, it's only required for deployment.
STATIC_ROOT
在开发过程中是没有用的,它只在部署时需要。
While in development, STATIC_ROOT
does nothing. You even don't need to set it. Django looks for static files inside each app's directory (myProject/appName/static
) and serves them automatically.
在开发中,STATIC_ROOT
什么都不做。你甚至不需要设置它。Django 在每个应用程序的目录 ( myProject/appName/static
) 中查找静态文件并自动提供它们。
This is the magic done by manage.py runserver
when DEBUG=True
.
这就是manage.py runserver
when的神奇之处DEBUG=True
。
Deployment
部署
When your project goes live, things differ. Most likely you will serve dynamic content using Django and static files will be served by Nginx. Why? Because Nginx is incredibly efficient and will reduce the workload off Django.
当您的项目上线时,情况就不同了。很可能您将使用 Django 提供动态内容,而静态文件将由 Nginx 提供。为什么?因为 Nginx 非常高效,并且会减少 Django 的工作量。
This is where STATIC_ROOT
becomes handy, as Nginx doesn't know anything about your django project and doesn't know where to find static files.
这是STATIC_ROOT
变得方便的地方,因为 Nginx 对您的 django 项目一无所知,也不知道在哪里可以找到静态文件。
So you set STATIC_ROOT = '/some/folder/'
and tell Nginx to look for static files in /some/folder/
. Then you run manage.py collectstatic
and Django will copy static files from all the apps you have to /some/folder/
.
所以你设置STATIC_ROOT = '/some/folder/'
并告诉 Nginx 在/some/folder/
. 然后你运行manage.py collectstatic
,Django 将从你必须的所有应用程序中复制静态文件/some/folder/
。
Extra directories for static files
静态文件的额外目录
STATICFILES_DIRS
is used to include additionaldirectories for collectstatic
to look for. For example, by default, Django doesn't recognize /myProject/static/
. So you can include it yourself.
STATICFILES_DIRS
用于包含要查找的其他目录collectstatic
。例如,默认情况下,Django 不识别/myProject/static/
. 所以你可以自己包含它。
Example
例子
STATIC_URL = '/static/'
if not DEBUG:
STATIC_ROOT = '/home/django/www-data/site.com/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]