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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:50:17  来源:igfitidea点击:

Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT

pythondjangodjango-staticfilesdjango-static

提问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_ROOTto 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.jsto STATIC_ROOT/admin/custom.js, but it is not working. Throwing 404 not found error.

我添加custom.jsSTATIC_ROOT/admin/custom.js,但它不起作用。抛出 404 not found 错误。

And then I change the STATIC_ROOTto STATICFILES_DIRS, and it works!!

然后我将其更改STATIC_ROOTSTATICFILES_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_ROOTand STATICFILES_DIRS.

所以,我不明白这里发生了什么。事实上,我只是不明白STATIC_ROOT和之间有什么区别STATICFILES_DIRS

Currently I am testing Django in my machine via virtualenv, not deployed yet, is it the reason STATIC_ROOTnot 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_ROOTis the folder where files uploaded using FileFieldwill go.

    Absolute filesystem path to the directory that will hold user-uploaded files.

  • STATIC_ROOTis the folder where static files will be stored after using manage.py collectstatic

    The absolute path to the directory where collectstaticwill collect static files for deployment.

    If the staticfilescontrib app is enabled (default) the collectstaticmanagement command will collect static files into this directory. See the howto on managing static files for more details about usage.

  • STATICFILES_DIRSis the list of folders where Django will search for additional static files aside from the staticfolder of each app installed.

    This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinderfinder is enabled, e.g. if you use the collectstaticor findstaticmanagement 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 应用程序将遍历的其他位置,例如,如果您使用collectstaticfindstatic管理命令或使用静态文件服务视图。

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.pynow.

正如settings.py现在在默认 Django 中定义的那样。

回答by pygaur

Difference between STATICFILES_DIRSand STATIC_ROOT

STATICFILES_DIRS和之间的区别STATIC_ROOT

The STATICFILES_DIRScan 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_ROOTdirectory should be empty and all static files should be collected into that directory.

如果您的 STATIC_ROOT 中当前有您希望提供服务的文件,那么您需要将这些文件移动到其他目录并将该其他目录放入STATICFILES_DIRS. 您的STATIC_ROOT目录应该是空的,并且所有静态文件都应该收集到该目录中。

MEDIA_ROOTwhere media files ,all uploaded files goes. Example : Images, Files

MEDIA_ROOT媒体文件,所有上传的文件在哪里。示例:图像、文件

回答by Max Malysh

Development

发展

STATIC_ROOTis useless during development, it's only required for deployment.

STATIC_ROOT在开发过程中是没有用的,它只在部署时需要。

While in development, STATIC_ROOTdoes 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 runserverwhen DEBUG=True.

这就是manage.py runserverwhen的神奇之处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_ROOTbecomes 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 collectstaticand 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_DIRSis used to include additionaldirectories for collectstaticto 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/'),
]