Django基础,Day7 - 添加静态文件 static files

添加css样式文件

1、首先在app目录下创建static文件夹,如polls/static。django会自动找到放在这里的静态文件。

AppDirectoriesFinder which looks for a “static” subdirectory in each of the INSTALLED_APPS

STATICFILES_DIRS:This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled

2、创建polls/static/polls/style.css

Because of how the AppDirectoriesFinder staticfile finder works, you can refer to this static file in Django simply as polls/style.css

1
2
3
li a {
     color green ;
}

3、在页面头部中引入css,引入方式  

1
2
3
{ %  load static  % }
 
<link rel = "stylesheet"  type = "text/css"  href = "{% static 'polls/style.css' %}"  / >

 {% static %} :生成静态资源static file的绝对url路径

polls/templates/polls/index.html:

4、刷新首页,可见链接内的文字颜色已变为green

添加背景图片

1、添加图片 polls/static/polls/images/background.jpg

2、添加css样式 polls/static/polls/style.css

1
2
3
body {
     background white  url ( "images/background.jpg" no-repeat  center  Top;
}

3、刷新首页,可见首页中添加了一张背景图片

 

分类:  Django
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值