一、html转义知识点
- 在html模板上下文中的html标记默认是会被转义的。
- 关闭转义的两种方法:
- {{ 模板变量|safe }}
- {% autoescape off %}
模板变量
{% endautoescape %}
二、代码演示
- 视图函数
def temp_escape(request):
return render(request, 'mytemplate/html_escape.html', {'context': '<h1>hello,world</h1>'})
- 模板文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>html转义</title>
</head>
<body>
视图传来的html标记默认会被转义---{{ context }}<br>
关闭转义:<br>
{{ context|safe }}<br>
关闭转义的另一种方法:<br>
{% autoescape off %}
{{ context }}
{% endautoescape %}<br>
模板硬编码默认不会转义 <br>
{{ test|default:'<h1>hello,world</h1>' }}<br>
手动转义 <br>
{{ test|default:'<h1>hello,world</h1>' }}<br>
</body>
</html>
三、遇到的问题:
1. default requires 2 arguments, 1 provided,原因在于default:后面不能空格
2.autoescape' tag requires exactly one argument, 少打了off