本文将讲解添加css文件和图片。
添加css文件
在polls目录下创建static目录,Django将自动从此目录下查找静态文件。
创建polls/static/polls/style.css如下:
li a {
color: green;
}
在polls/templates/polls/index.html中添加如下代码:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
其中的{% static %}模板标签生成静态文件的绝对路径。
此时访问http://localhost:8000/polls/将看到问题链接变成绿色。
添加图片
创建polls/static/polls/images目录,在其中放一张图片background.gif。
在polls/static/polls/style.css中添加:
body {
background: white url("images/background.gif") no-repeat;
}
此时访问http://localhost:8000/polls/将看到图片显示在屏幕左上角。