django 设置静态文件,static

一、搜集静态文件

1.1 命令行查看 collectstatic

guoguos-MacBook-Pro:mysite guoguo$ python manage.py -h

[staticfiles]
    collectstatic
    findstatic
    runserver

1.2 执行 python manage.py collectstatic 收集信息
guoguos-MacBook-Pro:mysite guoguo$ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settingsguoguo.

This will overwrite existing files!
Are you sure you want to do this?

Type ‘yes‘ to continue, or ‘no‘ to cancel: yes
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
    collected = self.collect()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect
    handler(path, prefixed_path, storage)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 354, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/management/commands/collectstatic.py", line 260, in delete_file
    if self.storage.exists(prefixed_path):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/core/files/storage.py", line 392, in exists
    return os.path.exists(self.path(name))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/staticfiles/storage.py", line 50, in path
    raise ImproperlyConfigured("You‘re using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You‘re using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
guoguos-MacBook-Pro:mysite guoguo$ python manage.py -h
1.3 解决报错 发现报错,在setting.py 中没有设置 STATIC_ROOT,ok,下面进行设置

在setting.py  底部设置配置 如下
STATIC_URL = ‘/static/‘
STATIC_ROOT = os.path.join(BASE_DIR, ‘static‘)

1.4 再次执行,查看结果
guoguos-MacBook-Pro:mysite guoguo$ python manage.py collectstatic

过程如下,这部分是把django 默认自带的 static 文件 搜集 放到该项目中,最后显示搜集62项
Copying ‘/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/admin/static/admin/css/base.css‘

Copying ‘/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/Django-1.11.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/xregexp/xregexp.min.js‘
Copying ‘/Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/polls/static/polls/style.css‘

62 static files copied to ‘/Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/static‘.

1.5 在 polls 项目发现有个多了个static 目录,里面显示上面搜集到的信息

└── static
    ├── admin
    │   ├── css
    │   │   ├── base.css
    │   │   ├── changelists.css
    │   │   ├── dashboard.css
    │   │   ├── fonts.css
    │   │   ├── forms.css
    │   │   ├── login.css
    │   │   ├── rtl.css
    │   │   └── widgets.css
    │   ├── fonts
    │   │   ├── LICENSE.txt
    │   │   ├── README.txt
    │   │   ├── Roboto-Bold-webfont.woff
    │   │   ├── Roboto-Light-webfont.woff
    │   │   └── Roboto-Regular-webfont.woff
    │   ├── img

    │           ├── jquery
    │           │   ├── LICENSE-JQUERY.txt
    │           │   ├── jquery.js
    │           │   └── jquery.min.js
    │           └── xregexp
    │               ├── LICENSE-XREGEXP.txt
    │               ├── xregexp.js
    │               └── xregexp.min.js

二、自定义static文件
 2.1 创建自定义static文件

│   ├── static
│   │   └── polls
│   │       ├── images
│   │       │   └── osd.png
│   │       └── style.css

2.2 编辑css 内容 设置 字体颜色和 背景图片
guoguos-MacBook-Pro:polls guoguo$ cat style.css
li a {
color: green;
}
body {
background: white url("images/osd.png") no-repeat right bottom;
}

2.3 编辑index.html文件内容

增加如下:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static ‘polls/style.css‘ %}" />
内容

guoguos-MacBook-Pro:polls guoguo$ pwd
/Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/polls/templates/polls
guoguos-MacBook-Pro:polls guoguo$ cat index.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static ‘polls/style.css‘ %}" />

{% if latest_question_list %}
<ul>
    {% for question in latest_question_list %}
<li>

     <a href="{% url ‘polls:detail‘ question.id %}"> {{question.question_text }} </a>

</li>
{% endfor %}
</ul>
{% else %}
<p> No polls are available.</p>
{% endif %}guoguos-MacBook-Pro:polls guoguo$ 

2.4 再次用python manage.py collectstatic 搜集信息
guoguos-MacBook-Pro:mysite guoguo$ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    /Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/static

This will overwrite existing files!
Are you sure you want to do this?

Type ‘yes‘ to continue, or ‘no‘ to cancel: yes
Copying ‘/Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/polls/static/polls/style.css‘

1 static file copied to ‘/Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/static‘, 61 unmodified.

2.5 查看结果   查看 polls程序是否更新

guoguos-MacBook-Pro:mysite guoguo$ python manage.py runserver
Performing system checks...
2.6 结果如下

时间: 2024-10-09 17:31:52

django 设置静态文件,static的相关文章

Django静态文件static的配置

Django静态文件static的配置 static文件夹的作用 ? static文件夹一般用于存放css,js,图片等静态文件 配置方式 ? 1.创建static文件夹 ? 2.在settings.py文件中新建一项(如果有则无需新建) STATIC_URL = '/static/' ? 3.在settings.py文件中新建一项 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] ? 4.引用静态文件中的文件 ? 列如引用一个cs

django中静态文件路径配置

#环境:ubuntu12.04+python27+django1.4.2+[ngix]+fastcgi; #如果经过ngix服务器的话无疑可以在任意位置放置,只要在ngix中配置相关的路径文件,但很多时候在调试时我们不想经过ngix服务器,那就需要对django进行静态文件的路径配置. #首先我想把我的静态文件放在项目的根目录下,创建一个static的文件夹,为了便于显示效果,我在static下放置了一个sa.txt文件,里面随意写一些内容! #文件目录如图所示: #打开settings.py文

django处理静态文件

转载自 http://www.cnblogs.com/wswang/p/5519101.html 静态文件指的是js css 还有图片这些,配置方法如下 1. 在设置文件(settings.py)中,installed_apps中添加 django.contrib.staticfiles 然后设置static_url 1 STATIC_URL = '/static/' 2. 在模板中的使用方式如下 1 2 {% load staticfiles %} <img src="{% static

django2.2 DEBUG=True/False时如何设置静态文件(image js css等)

目录结构: project ----templates ----app ----manage.py 添加静态资源,目录结构更新为: project ----templates ----app ----static # 静态资源 --------img --------js --------css ----manage.py 以img举例,引用资源的代码为: {% load static %} <img src='{% static "img/favicon.png" %}'/&g

apache上部署django的静态文件

一直在优化自己博客的代码, 昨天把css样式表分离出来, 用作静态 文件, 但是自己还没学django怎么使用静态文件, 经过一番google 终于解决了. django 使用静态文件有两种方法, 一种就是部署在django上, 另外一种 是部署在apache 上, 前一种方法有一定的缺陷, 效率不高, 而且会造 成一定的安全威胁, 具体我也不知道, 大家可以看官方文档, 但是还是 向大家介绍一下. 可以在你的项目下建一个static目录, 在settings.py 中添加: #填写你静态文件的

django的静态文件的引入

django的静态文件的引入 1.路径配置 在templates文件夹的同级目录下新建static文件夹 在setting里面写上STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] 2.静态文件的引入 硬编码 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <t

[django]windows下用Django,静态文件请求失败,出现UnicodeDecodeError

问题:windows下用Django,静态文件请求失败,出现UnicodeDecodeError:'utf-8' codec can't decode byte 0xb0 in position 1:invalid start byte,怎么解决啊? 回答:应该是静态文件的格式问题.也许它的编码是GBK的,你用UTF-8解,就解不开.用记事本打开,另存一个UTF-8格式.

Django 静态文件 static

一.概述: #静态文件交由Web服务器处理,Django本身不处理静态文件.简单的处理逻辑如下(以nginx为例): # URI请求-----> 按照Web服务器里面的配置规则先处理,以nginx为例,主要求配置在nginx. #conf里的location |---------->如果是静态文件,则由nginx直接处理 |---------->如果不是则交由Django处理,Django根据urls.py里面的规则进行匹配 # 以上是部署到Web服务器后的处理方式,为了便于开发,Dja

Python Django 之 静态文件存放设置

一.静态文件存放路径设置STATICFILES_DIRS 1.在django项目目录下面新建静态文件保存目录 2.在setting中添加相应寻找静态文件目录的配置 STATICFILES_DIRS=( os.path.join(BASE_DIR,"static"),) 3.将jquery放入相应路径中 4.验证静态文件的路径设置 1)url from django.contrib import adminfrom django.urls import pathfrom blog imp