autoescape:自动转义标签
在DTL模板中,默认开启了自动转义功能。
什么是自动转义功能呢?我们来通过一段代码展示一下:
首先, 写一个views.py文件,定义一个视图函数auto_escape
def auto_escape(request):
context={
"link":"<a href='http://www.baidu.com'>百度</a>"
}
return render(request,"auto.html",context=context)
然后在urls.py中做一个url映射
path('auto/', views.auto_escape,name="auto_escape"),
在写一个前端的页面auto.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>auto</title>
</head>
<body>
{{ link }}
<p>这是第二页</p>
</body>
</html>

9.png
可以看出来,这个百度链接被自动转译成一个字符串,
但是当我们使用
auto_space标签关闭DTL的自动转义功能之后
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>auto</title>
</head>
<body>
{% autoescape off %}
{{ link }}
{% endautoescape %}
<p>这是第二页</p>
</body>
</html>
原来的字符串被转换成了一个链接

10.png