第二十五节 autoescape标签

autoescape 标签可以用于转义,当从视图函数传入的字符串是一个标准的HTML语句时,出于保护机制,django会把它只当成一个字符串在页面直接显示,而不是作为HTML语句生效,如需使其生效,可用autoescape 将其关闭

views.py 代码

1 from django.shortcuts import render,HttpResponse,redirect,reverse
2 
3 def index(request):
4     context = {
5         'info':'<a href="http://www.baidu.com">百度</a>'
6     }
7     return render(request, 'index.html', context=context)

index.html 代码

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>模板渲染</title>
 6 </head>
 7 <body>
 8     {% autoescape off %}
 9         {{ info }}
10     {% endautoescape off %}
11 </body>
12 </html>
原文地址:https://www.cnblogs.com/kogmaw/p/12447787.html