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基础教程---JDK的安装和环境变量的配置

    一.Java的安装和环境变量配置 1.Java的安装: 第一步,从Oracle官网下载安装包,当然也可以从其他安全可靠的地方下载(PS:根据不同电脑系统下载相应的安装包,注意电脑的位数.如x64,x3 ...

  2. 初探ggplot2 geom__制作面积图

        大家大概都对如下信息图并不陌生,该图用100%堆积面积图的方式来表现不同时期不同国家人数所占的比例.这是一种很有意思的表达方式,而面积图也是很常用的数据图表,现在让我们一起来看看如何在R里用g ...

  3. setlocale 与 mbstowcs 的问题

    C++的字符串转换函数mbstowcs使用时容易产生bug... rapidxml_utils.hpp 的file(const char*filename)函数内会异常宕机... 需要在函数最开始添加 ...

  4. Android Retrofit使用教程(三):Retrofit与RxJava初相逢

    上一篇文章讲述了Retrofit的基本使用,包括GET,POST等请求.今天的文章中Retrofit要与RxJava配合使用. 了解RxJava RxJava有种种好处,我不在这里一一讲述.这里我只给 ...

  5. js右下角弹窗代码(实测好用)

    实测好用的js右下角弹窗代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  6. 如何破解linux用户帐号密码二

    转载 0x01 前言: 今天拿了个linux的主机,提下来了,以前提成root之后就没深入过,这次想着先把root密码破解出来: 以前交洞的时候只是单纯证明存在/etc/passwd和/etc/sha ...

  7. SilverLight.3-Validation:二、银光验证。TheLabel、TheDescriptionViewer和TheValidationSummary

    ylbtech-SilverLight.3-DataControls_BetterDataFroms:二.银光验证.TheLabel.TheDescriptionViewer和TheValidatio ...

  8. binary-tree-inorder-traversal——二叉树中序遍历

    Given a binary tree, return the inordertraversal of its nodes' values. For example:Given binary tree ...

  9. C# DateTime和String(转)

    http://www.cnblogs.com/Pickuper/articles/2058880.html C#语言之“string格式的日期时间字符串转为DateTime类型”的方法 方法一:Con ...

  10. iOS常用的加密方式

    MD5 iOS代码加密 创建MD5类,代码如下 #import <Foundation/Foundation.h> @interface CJMD5 : NSObject +(NSStri ...