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的更多相关文章

  1. django 设置静态文件,static 链接

    这篇文章讲的django 静态static 文件设置,还可以,供参考 http://blog.csdn.net/sinat_21302587/article/details/74059078

  2. django处理静态文件

    静态文件指的是js css 还有图片这些,配置方法如下 1. 在设置文件(settings.py)中,installed_apps中添加 django.contrib.staticfiles 然后设置 ...

  3. Django之静态文件,中间件,admin后台管理

    静态文件 静态文件的使用 在 网页使用的css文件,js文件和图片等叫做静态文件.1)在项目下新建静态文件夹 static. 2) 配置静态文件所在的物理目录.Settings.py STATIC_U ...

  4. [Django框架 - 静态文件配置、request对象方法初识、 pycharm链接数据库、ORM实操增删改查、django请求生命周期]

    [Django框架 - 静态文件配置.request对象方法初识. pycharm链接数据库.ORM实操增删改查.django请求生命周期] 我们将html文件默认都放在templates文件夹下 将 ...

  5. Django中静态文件引用优化

    静态文件引用优化 在html文件中是用django的静态文件路径时,一般会这么写: <script type="text/javascript" src="/sta ...

  6. django的静态文件的引入

    django的静态文件的引入 1.路径配置 在templates文件夹的同级目录下新建static文件夹 在setting里面写上STATICFILES_DIRS = [os.path.join(BA ...

  7. apache上部署django的静态文件

    一直在优化自己博客的代码, 昨天把css样式表分离出来, 用作静态 文件, 但是自己还没学django怎么使用静态文件, 经过一番google 终于解决了. django 使用静态文件有两种方法, 一 ...

  8. 管理Django1.9静态文件static

    管理Django1.9静态文件static 网站通常需要增加图片.JavaScript.或者CSS等文件提供服务.在Django中,我们把这些文件称为“静态文件”(static files).Djan ...

  9. gunicorn启动django时静态文件的加载

    目前在用nginx+gunicorn对django进行部署 当我用gunicorn -w 4 -b 127.0.0.1:8080 myproject.wsig:application启动django时 ...

随机推荐

  1. java 读写操作

    java代码: 写入: public void getNotice(HttpServletRequest request, String notice){ String message = JSON. ...

  2. LeakCanary——直白的展现Android中的内存泄露

    之前碰到的OOM问题,终于很直白的呈现在我的眼前:我尝试了MAT,但是发现不怎么会用.直到今天终于发现了这个新工具: 当我们的App中存在内存泄露时会在通知栏弹出通知: 当点击该通知时,会跳转到具体的 ...

  3. [置顶] zabbix发送告警

    之前使用邮件和短信发送zabbix告警信息,但告警信息无法实时查看或者无法发送,故障无法及时通知运维人员. 后来使用第三方微信接口发送信息,愉快地用了一年多,突然收费了. zabbix告警一直是我的痛 ...

  4. HDU1565方格取数

    典型的状态压缩DP问题.第i行的取法只受到第i-1行的影响.首先每一行的取法要相容(不能有两个相邻),然后相邻行之间也要相容.将每一个格子看做两种状态,1表示取,0表示不取.这样每一行就是一个01串, ...

  5. python 浮点数保留小数

    http://www.cnblogs.com/Raymon-Geng/p/5784290.html 这里有三种方法, round(a,2) '%.2f' % a Decimal('5.000').qu ...

  6. Source tree配置gitlab

    1.打开控制台:ssh-keygen -t rsa -C "GIT上的账号邮箱" 2.回车 3.输入密码(git上的账号密码) 4.确认密码 5.输入命令 cd .ssh 6.输入 ...

  7. Jinja2文档学习

    这些文档需要精度一遍 1.http://jinja.pocoo.org/docs/dev/ 2.http://jinja.pocoo.org/docs/dev/templates/# 3.https: ...

  8. Android - 显示手机执行的Activity

    显示手机执行的Activity 本文地址:http://blog.csdn.net/caroline_wendy 手机中,须要调试程序的界面,能够高速进行定位,使用Android开发工具ADB(And ...

  9. 关于阿里 weex 的使用与案例

    1. 阿里宣布开源Weex http://mp.weixin.qq.com/s?__biz=MzA4MjA0MTc4NQ==&mid=504089541&idx=1&sn=3a ...

  10. Mark一下, dp状态转移方程写对,可是写代码都错,poj 1651 poj 1179

    dp题: 1.写状态转移方程; 2.考虑初始化边界,有意义的赋定值.还没计算的赋边界值: 3.怎么写代码自底向上计算最优值 今天做了几个基础dp,所有是dp方程写对可是初始化以及计算写错 先是poj ...