Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
作者:SpecYue 时间:2023-11-11 06:41:39
本文实例讲述了Django框架模板语言。分享给大家供大家参考,具体如下:
模板语言
模板语言简称为DTL(Django Template Language)
模板变量
模板变量名由数字,字母,下划线和点组成,不能以下划线开头。
使用:{{模板变量名}}
def index2(request):
'''模板加载顺序'''
return render(request, 'booktest/index2.html')
# /temp_var
def temp_var(request):
'''模板变量'''
my_dict = {'title': '字典键值'}
my_list = [1, 2, 3]
book = BookInfo.objects.get(id=1)
#定义模板上下文
context={'my_dict':my_dict,'my_list':my_list,'book':book}
return render(request,'booktest/temp_var.html',context)
模板变量可以是字典,列表或者对象。定义好模板上下文之后,用render()
函数传递给html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板变量</title>
</head>
<body>
使用字典属性:{{ my_dict.title }}
使用列表元素:{{ my_list.1 }}
使用对象属性:{{ book.btitle }}
</body>
</html>
可以看到模板变量都是通过 . 调用的。
模板标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板标签</title>
<style>
.red{
background-color: red;
}
.yellow{
background-color: yellow;
}
.green{
background-color: green;
}
</style>
</head>
<body>
<ul>
{% for book in books %}
{% if book.id <= 2 %}
<li class="red">{{ forloop.counter }}--{{ book.btitle }}</li>
{% elif book.id >= 5 %}
<li class="yellow">{{ forloop.counter }}--{{ book.btitle }}</li>
{% else %}
<li class="green">{{ forloop.counter }}--{{ book.btitle }}</li>
{% endif %}
{% endfor %}
</ul>
</body>
</html>
具体的其他的模板标签可以参考Django官方文档。
过滤器
过滤器用于对模板变量进行操作
date:改变日期的显示格式
length:求长度,字符串,列表,元祖,字典
default:设置模板变量的默认值
格式:模板变量 | 过滤器:参数
date过滤器
<li class="red">{{ book.btitle }}--{book.bpub_date | date:'Y年-m月-d日'}</li>
default过滤器 {{dd | default:'无'}}
模板注释
单行注释:{# 注释 #}
多行注释:{% comment %}
模板继承
不同页面可能有相同的模块,这时候可以使用模板继承减少代码量
base.html内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父模板</title>
</head>
<body>
<h1>导航条</h1>
{% block b1 %}
<h1>这是父模板b1块中的内容</h1>
{% endblock b1 %}
<h1>版权信息</h1>
</body>
</html>
child.html内容
{% extends 'booktest/base.html' %}
{% block b1 %}
{{ block.super }}
<h1>这是子模板b1的内容</h1>
{% endblock b1 %}
在父模板中{% block b1 %} <h1>这是父模板b1块中的内容</h1> {% endblock b1 %}
定义一个预留快,预留块中可以有内容。子模板继承时,{% extends 'booktest/base.html' %}
导入,{% block b1 %} {{ block.super }} <h1>这是子模板b1的内容</h1> {% endblock b1 %}
写预留块,{{ block.super }}
继承预留快的内容。
html转义
通过render()
函数传递过来的模板上下文默认是转义的,也就是说我们想传递html语言的时候,实际上传递过来的是字符串,这个时候我们可以通过过滤器关闭转义
{{context | safe}}
希望本文所述对大家基于Django框架的Python程序设计有所帮助。
来源:https://blog.csdn.net/qq_34788903/article/details/87907182
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java使用正则表达式验证用户名和密码的方法
Python中import机制详解
python config文件的读写操作示例
Python抓包程序mitmproxy安装和使用过程图解
![](https://img.aspxhome.com/file/2023/9/66879_0s.png)
详解Python中的正斜杠与反斜杠
python selenium参数详解和实现案例
![](https://img.aspxhome.com/file/2023/1/62881_0s.png)
用python实现爬取奥特曼图片实例
![](https://img.aspxhome.com/file/2023/4/112154_0s.jpg)
javaScript产生随机数的用法小结
Python 远程开关机的方法
![](https://img.aspxhome.com/file/2023/6/124416_0s.jpg)
使用css2.1实现多重背景、多重边框效果[译]
![](https://img.aspxhome.com/file/UploadPic/20108/23/css2-multiple-background-border-model-12s.png)
vue中自定义指令(directive)的基本使用方法
![](https://img.aspxhome.com/file/2023/2/123192_0s.png)
Python中bisect的使用方法
mysql installer community 8.0.12.0安装图文教程
![](https://img.aspxhome.com/file/2023/3/115573_0s.jpg)
使用Python解决常见格式图像读取nii,dicom,mhd
请给PNG8一个机会
Python利用3D引擎写一个Pong游戏
![](https://img.aspxhome.com/file/2023/4/131704_0s.png)