<i id='Le9c5'><tr id='Le9c5'><dt id='Le9c5'><q id='Le9c5'><span id='Le9c5'><b id='Le9c5'><form id='Le9c5'><ins id='Le9c5'></ins><ul id='Le9c5'></ul><sub id='Le9c5'></sub></form><legend id='Le9c5'></legend><bdo id='Le9c5'><pre id='Le9c5'><center id='Le9c5'></center></pre></bdo></b><th id='Le9c5'></th></span></q></dt></tr></i><div id='Le9c5'><tfoot id='Le9c5'></tfoot><dl id='Le9c5'><fieldset id='Le9c5'></fieldset></dl></div>
    <legend id='Le9c5'><style id='Le9c5'><dir id='Le9c5'><q id='Le9c5'></q></dir></style></legend>

      <small id='Le9c5'></small><noframes id='Le9c5'>

      <tfoot id='Le9c5'></tfoot>
        <bdo id='Le9c5'></bdo><ul id='Le9c5'></ul>
      1. STATICFILES_DIR、STATIC_ROOT 和 MEDIA_ROOT 的区别

        时间:2024-04-21
        • <tfoot id='TLW1b'></tfoot>
              <bdo id='TLW1b'></bdo><ul id='TLW1b'></ul>
                <legend id='TLW1b'><style id='TLW1b'><dir id='TLW1b'><q id='TLW1b'></q></dir></style></legend>

                  <tbody id='TLW1b'></tbody>

                  <i id='TLW1b'><tr id='TLW1b'><dt id='TLW1b'><q id='TLW1b'><span id='TLW1b'><b id='TLW1b'><form id='TLW1b'><ins id='TLW1b'></ins><ul id='TLW1b'></ul><sub id='TLW1b'></sub></form><legend id='TLW1b'></legend><bdo id='TLW1b'><pre id='TLW1b'><center id='TLW1b'></center></pre></bdo></b><th id='TLW1b'></th></span></q></dt></tr></i><div id='TLW1b'><tfoot id='TLW1b'></tfoot><dl id='TLW1b'><fieldset id='TLW1b'></fieldset></dl></div>
                1. <small id='TLW1b'></small><noframes id='TLW1b'>

                  本文介绍了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.

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

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

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

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

                5. 在您的设置中,您应该:

                  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 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在Django中调试,好方法? 下一篇:“要解压的值太多"例外

                  相关文章

                  <tfoot id='STHCZ'></tfoot>

                  <small id='STHCZ'></small><noframes id='STHCZ'>

                  <legend id='STHCZ'><style id='STHCZ'><dir id='STHCZ'><q id='STHCZ'></q></dir></style></legend>
                  <i id='STHCZ'><tr id='STHCZ'><dt id='STHCZ'><q id='STHCZ'><span id='STHCZ'><b id='STHCZ'><form id='STHCZ'><ins id='STHCZ'></ins><ul id='STHCZ'></ul><sub id='STHCZ'></sub></form><legend id='STHCZ'></legend><bdo id='STHCZ'><pre id='STHCZ'><center id='STHCZ'></center></pre></bdo></b><th id='STHCZ'></th></span></q></dt></tr></i><div id='STHCZ'><tfoot id='STHCZ'></tfoot><dl id='STHCZ'><fieldset id='STHCZ'></fieldset></dl></div>
                        <bdo id='STHCZ'></bdo><ul id='STHCZ'></ul>