STATICFILES_DIR、STATIC_ROOT 和 MEDIA_ROOT 的区别

Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT(STATICFILES_DIR、STATIC_ROOT 和 MEDIA_ROOT 的区别)

本文介绍了STATICFILES_DIR、STATIC_ROOT 和 MEDIA_ROOT 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这三个静态url有什么区别?

What are the differences of these three static url?

我不确定我是否正确,我正在使用 MEDIA_ROOT 来存储我上传的照片(通过 models.ImageField())

I am not sure if I am right, I am using the MEDIA_ROOT to store my uploaded photos (via models.ImageField())

但是,我在 admin.py 中为我的管理员创建了一个 JS 脚本.我将媒体定义如下:

However, I created a JS script to my admin and in admin.py. I defined the media as below:

....
class Media:
      js = ('/admin/custom.js', )

还有我的settings.py:

 ....
 STATIC_ROOT = "/home/user/project/django1/top/listing/static"

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

and I added the custom.js to STATIC_ROOT/admin/custom.js, but it is not working. Throwing 404 not found error.

然后我把STATIC_ROOT改成STATICFILES_DIRS,就可以了!!

And then I change the STATIC_ROOT to STATICFILES_DIRS, and it works!!

....
STATICFILES_DIRS = "/home/user/project/django1/top/listing/static"

所以,我不明白这里发生了什么.其实我只是不明白STATIC_ROOTSTATICFILES_DIRS有什么区别.

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.

目前我正在通过 virtualenv 在我的机器上测试 Django,尚未部署,这是 STATIC_ROOT 不起作用的原因吗?

Currently I am testing Django in my machine via virtualenv, not deployed yet, is it the reason STATIC_ROOT not working??

推荐答案

您可以在 Django 文档.以下是我自己对文档的定义和引用:

You can find these settings in the Django documentation. Here are my own definitions and quotations from the documentation:

  • MEDIA_ROOT 是使用 FileField 上传的文件所在的文件夹.

  • MEDIA_ROOT is the folder where files uploaded using FileField will go.

存放用户上传文件的目录的绝对文件系统路径.

  • STATIC_ROOT是使用manage.py collectstatic

    collectstatic 将收集用于部署的静态文件的目录的绝对路径.

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

    如果 staticfiles contrib 应用程序已启用(默认),collectstatic 管理命令会将静态文件收集到此目录中.有关使用的更多详细信息,请参阅管理静态文件的操作指南.

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

  • STATICFILES_DIRS 是 Django 将在其中搜索除每个已安装应用程序的 static 文件夹之外的其他静态文件的文件夹列表.

  • STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.

    如果启用了 FileSystemFinder 查找器,则此设置定义静态文件应用程序将遍历的其他位置,例如如果您使用 collectstaticfindstatic 管理命令或使用静态文件服务视图.

    This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

  • 在您的设置中,您应该:

    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", )
    

    ...哪里:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    

    现在在默认的 Django settings.py 中定义.

    as defined in the default Django settings.py now.

    这篇关于STATICFILES_DIR、STATIC_ROOT 和 MEDIA_ROOT 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:STATICFILES_DIR、STATIC_ROOT 和 MEDIA_ROOT 的区别

    基础教程推荐