STATICFILES_DIR,STATIC_ROOT和MEDIA_ROOT之间的差异 [英] Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT

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

问题描述

这三个静态网址有什么区别?



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



然而,我创建了一个JS脚本给我的管理员,并在 admin.py 。我定义了如下媒体:

  .... 
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找不到错误。



然后我将 STATIC_ROOT 更改为 STATICFILES_DIRS 它的作品!!

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

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



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

解决方案

您可以找到每个这些设置的定义在文档中。这是一个快速定义(我自己的)和每个文档的引用:




  • MEDIA_ROOT 是每个文件上传的文件夹,其中 FileField 将上传。


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



  • STATIC_ROOT 每个静态文件将存储在一个 manage.py collectstatic


    绝对路径到 collectstatic 将收集静态文件进行部署的目录。



    如果 staticfiles contrib应用程序启用(默认) collectstatic 管理命令将静态文件收集到此目录中。有关使用情况的详细信息,请参阅管理静态文件的方法。



  • STATICFILES_DIRS 是除了每个安装的每个应用程序的每个静态文件夹之外,Django会搜索其他静态文件的文件夹列表。


    如果启用了 FileSystemFinder finder,则此设置定义了staticfiles应用程序将遍历的其他位置,例如如果您使用 collectstatic findstatic 管理命令或使用静态文件服务视图。








在你的设置中,你应该有: p>

  MEDIA_ROOT = os.path.join(BASE_DIR,media /)
STATIC_ROOT = os.path.join(BASE_DIR ,static /)

STATICFILES_DIRS =(/ home / user / project / django1 / top / listing / static,)#创建一个字符串元组,而不是一个字符串

...其中 BASE_DIR = os.path.dirname(os.path.dirname(os。 path.abspath(__ file __))),如默认Django settings.py 模板中定义的。


What are the differences of these three static url?

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

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

and my 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.

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

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

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

解决方案

You can find a definition for each of these settings in the documentation. Here is a quick definition (of my own) and a quotation of the doc, for each:

  • MEDIA_ROOT is the folder where every files uploaded with an FileField will go.

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

  • STATIC_ROOT is the folder where every static files will be stored after a manage.py collectstatic

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

    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 is the list of folder where Django will search for additional static files, in addition to each static 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 the collectstatic or findstatic management command or use the static file serving view.


In your setting, you should have:

MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATICFILES_DIRS = ("/home/user/project/django1/top/listing/static", )  # Make a tuple of strings instead of a string

... where BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))), as defined in the default Django settings.py template now.

这篇关于STATICFILES_DIR,STATIC_ROOT和MEDIA_ROOT之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆