django 设置静态文件,static
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 , 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 , 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 , 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 , 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 , 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 , 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 , 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 , 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 , 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 , 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 , 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' 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' static file copied to '/Users/guoguo/Documents/GitHub/zuopiezi/python_project/develop_py/mysite/static', unmodified. 2.5 查看结果 查看 polls程序是否更新 guoguos-MacBook-Pro:mysite guoguo$ python manage.py runserver
Performing system checks...
2.6 结果如下

django 设置静态文件,static的更多相关文章
- django 设置静态文件,static 链接
这篇文章讲的django 静态static 文件设置,还可以,供参考 http://blog.csdn.net/sinat_21302587/article/details/74059078
- django处理静态文件
静态文件指的是js css 还有图片这些,配置方法如下 1. 在设置文件(settings.py)中,installed_apps中添加 django.contrib.staticfiles 然后设置 ...
- Django之静态文件,中间件,admin后台管理
静态文件 静态文件的使用 在 网页使用的css文件,js文件和图片等叫做静态文件.1)在项目下新建静态文件夹 static. 2) 配置静态文件所在的物理目录.Settings.py STATIC_U ...
- [Django框架 - 静态文件配置、request对象方法初识、 pycharm链接数据库、ORM实操增删改查、django请求生命周期]
[Django框架 - 静态文件配置.request对象方法初识. pycharm链接数据库.ORM实操增删改查.django请求生命周期] 我们将html文件默认都放在templates文件夹下 将 ...
- Django中静态文件引用优化
静态文件引用优化 在html文件中是用django的静态文件路径时,一般会这么写: <script type="text/javascript" src="/sta ...
- django的静态文件的引入
django的静态文件的引入 1.路径配置 在templates文件夹的同级目录下新建static文件夹 在setting里面写上STATICFILES_DIRS = [os.path.join(BA ...
- apache上部署django的静态文件
一直在优化自己博客的代码, 昨天把css样式表分离出来, 用作静态 文件, 但是自己还没学django怎么使用静态文件, 经过一番google 终于解决了. django 使用静态文件有两种方法, 一 ...
- 管理Django1.9静态文件static
管理Django1.9静态文件static 网站通常需要增加图片.JavaScript.或者CSS等文件提供服务.在Django中,我们把这些文件称为“静态文件”(static files).Djan ...
- gunicorn启动django时静态文件的加载
目前在用nginx+gunicorn对django进行部署 当我用gunicorn -w 4 -b 127.0.0.1:8080 myproject.wsig:application启动django时 ...
随机推荐
- 调试SQLSERVER (一)生成dump文件的方法
http://www.cnblogs.com/lyhabc/p/4184149.html http://www.cnblogs.com/lyhabc/p/4185399.html
- mysql老司机之路
MYSQL数据库基础: 数据库帮我们解决以下数据存取难题: 较大数据量 事务控制 持久化和数据安全 高性能要求 高并发访问 关系型:mysql,oracle,sql server,postgresql ...
- Start Developing iOS Apps Today
view types - view常见类型
- AIX 删除指定目录、指定后缀、指定天数以前的历史文件
find /home/oracle/admin/zhjport/udump/ -name "*.trc" -mtime +30 -exec rm {} \; 如果要自动执行可以加入 ...
- RHEL CentOS Fedora各种源介绍和安装
CentOS默认自带CentOS-Base.repo源,但官方源中去除了很多有版权争议的软件,而且安装的软件也不是最新的稳定版. 下面介绍各种第三方软件库,以下软件库适用于与RHEL完全兼容的li ...
- FTP的主动模式与被动模式
FTP服务器使用20和21两个网络端口与FTP客户端进行通信. FTP服务器的21端口用于传输FTP的控制命令,20端口用于传输文件数据. FTP主动模式: FTP客户端向服务器的FTP控制端口(默认 ...
- ylb: SQL表的高级查询-子查询
ylbtech-SQL Server: SQL Server- SQL表的高级查询-子查询 SQL Server 表的高级查询-子查询. 1,ylb:表的高级查询-子查询返回顶部 --======== ...
- js 模拟发短信
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- HDU 1017 A Mathematical Curiosity (枚举水题)
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...
- mongodb分片片键的选择(持续更新中)
首先要了解项目的情况,检查使用情况 对集合进行分片时,要选择一个或者两个字段拆分数据,这个键叫做片键 一旦拥有对个分片,在修改片键几乎是不肯能的事情,因此选择合适的片键是非常重要的. 对集合分片之前要 ...