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. shell实践--shell内嵌指令实现查看上线时间

    实践小点子: 1. 做一个shell 内嵌指令例如:ls,cd,pwd这样     就实现查看上线多久:   解决办法有两种: 1) 利用脚本,如新指令为look;利用别名的方法,将look.sh脚本 ...

  2. HTTP Range - [Web开发]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://minms.blogbus.com/logs/39569593.html 所谓 Range,是在 HTTP/1.1(htt ...

  3. PathInterpolator

    PathInterpolator 在v4 support library:Revision 22.1.0的时候,Google在兼容库中增加了几个新的类,用于创建更加真实的动画效果. Added the ...

  4. Flutter开发记录part2

    (1)文本超出折叠:child: Text('跑马灯dddd的范德萨范德萨放多少范德萨范德萨范德萨范德萨范德萨发',maxLines: 1,overflow: TextOverflow.ellipsi ...

  5. 在C#中使用C++编写的类——用托管C++进行封装[转]

    现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前搞VC++开发的人都转向用更强大的VS.Net.在这种情况 下,有很多开发人员就面临了如何在C#中使用C++开发好的 ...

  6. maven 配置jetty插件

    <build>        <finalName>shiroweb</finalName>        <plugins>            & ...

  7. remove-duplicates-from-sorted-list I&II——去除链表中重复项

    I.Given a sorted linked list, delete all duplicates such that each element appear only once. For exa ...

  8. code::blocks(版本号10.05) 配置opencv2.4.3

    (1)首先下载opencv2.4.3, 解压缩到D:下: (2)配置code::blocks, 详细操作例如以下: 第一步, 配置compiler, 操作步骤为Settings  -> Comp ...

  9. HTTP头解读

    Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET.POST.PUT.DELETE.一个URL地址用于描述一个网络上的资源, 而HTTP中的GET.POST.PUT. DELETE ...

  10. SpringBoot学习之常用注解

    @SpringBootAppliaction:通常注解写在SpringBoot启动类中,主要包括三个作用: 1.@Configuration表示将该类作用springboot配置文件类. 2.@Ena ...