美文网首页django
Django settings.py中的静态文件管理设置

Django settings.py中的静态文件管理设置

作者: 第八共同体 | 来源:发表于2017-01-20 17:03 被阅读1560次

1. STATIC_ROOT

官方文档上是这么说的:

Default: None
The absolute path to the directory where [collectstatic]  
 (https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/#django-admin-collectstatic)  
 will collect static files for deployment. Example: "/var/www/example.com/static/"

说的是啥呢,就是说:这个字段的的目录路径是用来为部署而收集静态文件的地方。
更具体的说呢,当我们执行python manage.py collectstatic命令的时候,系统会帮我们把所有的静态文件都收集到该目录下。
很多时候,我们在settings.py文件中设置该字段时,是这么设置的:

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

但是你也可以设置为任何你想要设置的目录,比如/var/www/example.com/static/
注意:一定是绝对路径哦。

2.STATIC_URL

首先。po出官方文档的说法:

Default: None

URL to use when referring to static files located in [STATIC_ROOT]  
(https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATIC_ROOT).  
Example: "/static/" or "http://static.example.com/"

If not None , this will be used as the base path for [asset definitions]  
(https://docs.djangoproject.com/en/1.10/topics/forms/media/#form-asset-paths) 
(the Media class) and the [staticfiles app]  
(https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/).
It must end in a slash if set to a non-empty value.

说的就是,这个字段的值可以为空,也可以为一个string类型的url.主要作用就是当我们想要引用在STATIC_ROOT中静态文件时可以使用该url值,有图有真相:

Image 1.png Image 3.png

当然了,你也可以把它设置为和STATIC_ROOT相同的路径值。还有留意一下官文中最后一句话:

It must end in a slash if set to a non-empty value.

就是说,如果该字段值不为空,必须在最后加上一个斜杠,那下面我就不加,看看会有什么效果。

Image 4.png
直接报错了,看来这个斜杠是必须要加的

3.STATICFILES_DIRS

我们依然先po出文档:

Default: []
 (Empty list)
This setting defines the additional locations the staticfiles app will traverse
 if the FileSystemFinder finder is enabled, e.g. if you use the[collectstatic]  
(https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/#django-admin-collectstatic) 
or [findstatic](https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/#django-admin-findstatic) 
management command or use the static file serving view.
This should be set to a list of strings that contain full paths to your additional files directory(ies)
 e.g.: STATICFILES_DIRS = [ "/home/special.polls.com/polls/static",
                   "/home/polls.com/polls/static", 
                   "/opt/webfiles/common",]

Note that these paths should use Unix-style forward slashes, 
even on Windows (e.g. "C:/Users/user/mysite/extra_static_content" ).

Prefixes (optional) (https://docs.djangoproject.com/en/1.10/ref/settings/#prefixes-optional)
In case you want to refer to files in one of the locations with an additional namespace, 
you can **optionally** provide a prefix as (prefix,path)  tuples, e.g.:
STATICFILES_DIRS = [ 
            # ... 
           ("downloads", "/opt/webfiles/stats"),]

For example, assuming you have [STATIC_URL]  
(https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATIC_URL)
 set to '/static/' , the [collectstatic]  
(https://docs.djangoproject.com/en/1.10/ref/contrib/staticfiles/#django-admin- collectstatic) 
management command would collect the “stats” files in a 'downloads' subdirectory of 
[STATIC_ROOT ](https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATIC_ROOT).  
This would allow you to refer to the local file '/opt/webfiles/stats/polls_20101022.tar.gz'
 with '/static/downloads/polls_20101022.tar.gz' in your templates,
 e.g.:
         <a href="{% static "downloads/polls_20101022.tar.gz" %}">

默认是一个空列表,如果我们开启了FileSystemFinder ,那么这个设置定义了staticfiles app将会遍历的一个附加的位置信息。
该值应该设置为一个字符串的列表形式,每个元素都是附加文件目录的绝对路径。
注意):这些路径都应该使用unix风格的斜杠,即便是在windows平台上("C:/Users/user/mysite/extra_static_content")
可选的前缀:如果你想在一个有附加命名空间的位置中引用文件,你可以使用一个元祖,把前缀加上。比如:现在我们的STATIC_URL的值设置为'/static/',STATICFILES_DIRS 设置为

STATICFILES_DIRS = [ 
            # ... 
           ("downloads", "/opt/webfiles/stats"),]

那么在执行collectstatic命令后,所有stats文件都将收在STATIC_ROOT的子目录downloads中。同时,当我们引用静态文件的时候,也可以直接使用/static/downloads/polls/...,而不使用opt/webfiles/stats/polls/...
如果还没看懂,下面我把图片例子po出来。

Image 5.png
然后执行命令python manage.py collectstatic Image 6.png

引用静态文件的时候,可以使用下面的方式:

Image 7.png

4.MEDIA_ROOT

该设置项可以用来设置用户上传的文件的存放位置。值为路径字符串。

MEDIA_ROOT = os.path.join(BASE_DIR,'media')

举个例子
我在models中,实现一个model类

class ModelWithFileField(models.Model):
      file = models.FileField(upload_to='uploads/file/')
      upload = models.FileField(upload_to='uploads/%Y/%m/%d/')

字段的upload_to参数指定文件上传的位置。
在进行上传操作后,通过文件结构的变化可以发现发生了什么

image.png

5.待说明的地方:

  • 在settings.py中设置STATICFILES_DIRS和STATIC_ROOT的路径值不能相同,如果设置相同在执行python manage.py collectstatic会报错误。

相关文章

网友评论

    本文标题:Django settings.py中的静态文件管理设置

    本文链接:https://www.haomeiwen.com/subject/kbtgbttx.html