Django模板标签
接上一节 : https://blog.youkuaiyun.com/LeeSkyRa/article/details/103735329
相对上一节,app_st/views.py 文件和 templates/app_st/variable.py 文件代码做了部分删除和修改,并在 templates/app_st 目录下新建了一个广告模板 ad.html 文件
templates/app_st/ad.html 文件代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>广告</title>
<style>
a{
text-decoration: none;
}
a:hover{
text-decoration: none;
}
#box_ad{
position: fixed;
bottom: 0;
}
</style>
</head>
<body>
<div id="box_ad">
<h1><a id="a1" href="http://www.taobao.com">
我是某宝广告!不要点,不要点!</a></h1>
</div>
<script>
var h=document.getElementById('a1');
var color='blue';
function change_color() {
if (color=='blue'){
color='red';
}else {
color='blue';
}
h.style.color=color;
setTimeout('change_color()',200)
}
change_color()
</script>
</body>
</html>
app_st/views.py 文件代码如下:
from django.shortcuts import render
from django.http import HttpResponse
from datetime import datetime
# Create your views here.
def view_st(request):
return HttpResponse('<h1>这是我的第一个 django 视图</h1>')
def home(request):
return render(request,'app_st/home.html')
def base(request):
return render(request,'app_st/base.html')
def variable(request):
lee={
'name':'天兔',
'age':20,
'sex':'M',
}
li=[1,2,3,4,5,6,7,8,9,0]
return render(request,'app_st/variable.html',context={
'lee':lee,
'li':li,
})
templates/app_st/variable.py 文件代码如下:
{% extends 'app_st/base.html' %}
{% load myfilter %}
{% load static %}
{% block title %}VARIABLE{% endblock %}
{% block css %}{% static 'app_st/css/variable.css' %}{% endblock %}
{% block content %}
<div id="box_content">
{% for key,val in lee.items %}
<p>我的 {{ key }} 是 : {{ val }}</p>
{% endfor %}
<hr>
{% for a in li %}
{% if a > 0 %}
<p>列表第 {{ a }} 个元素是 : {{ a }}</p>
{% elif a == 0 %}
<p>列表第 {{ a | add:10 }} 个元素是 : {{ a }}</p>
{% endif %}
{% endfor %}
</div>
{% endblock %}
{% block ad %}
{% include 'app_st/ad.html' %}
{% endblock %}
下面,启动 django 服务,浏览器地址栏输入 127.0.0.1:8000/app/variable 查看效果