标签:efault models str end nbsp lan 转换 escape contex
< 会转换为<
> 会转换为>
‘ (单引号) 会转换为'
" (双引号)会转换为 "
& 会转换为 &
views.py
from django.shortcuts import render from booktest.models import * def htmlTest(request): context = {‘h1‘: ‘<h1>123</h1>‘} return render(request, ‘booktest/htmlTest.html‘, context)
配置好urls
htmlTest.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {{ h1|escape }} {# 等价于{{ h1 }} #} <hr> {{ h1|safe }} <hr> {% autoescape off %} {# 等价于{{ h1|safe }} #} {{ h1 }} {% endautoescape %} <hr> 字面值:<br> {{ t2|default:‘<h1>abc</h1>‘ }} {# 输出结果是大写的abc #} <hr> 手动转义:<br> {{ t2|default:‘<h1>abc</h1>‘ }} {# 注意写在views.py 和写在htmlTest用过滤器的区别 #} </body> </html>
标签:efault models str end nbsp lan 转换 escape contex
原文地址:https://www.cnblogs.com/gaota1996/p/10444122.html