Python: django字符串中HTML防止转义/模版关闭转义的方法Python: django字符串中HTML防止转义/模版关闭转义的方法Python: django字符串中HTML防止转义/模版关闭转义的方法Python: django字符串中HTML防止转义/模版关闭转义的方法
  • 首页
  • 博客
  • 文件
  • 书签
  • 分析
  • 登录
Search
Generic filters

Python: django字符串中HTML防止转义/模版关闭转义的方法

Published by admin at 2022年7月7日
Categories
  • Django
Tags
1#.Python代码中
from django.utils.safestring import mark_safe

def index(request):
    s = "<h1>index page</h1>"
    s = mark_safe(s)
    return render(request, "app01/index.html", {"h1Tag": s}
#---------------
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>app01 index</title>
</head>
<body>
{{ h1Tag }}
</body>
</html>

2#.template标签和过滤器

safe过滤器和autoescape可以控制,是否将字符串渲染成html代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>app01 index</title>
</head>
<body>
{# 默认情况: #}
{{ h1Tag }}
{# <h1>index page</h1> #}

{# 第一种方法 #}
{{ h1Tag | safe }}

{# 第二种方法 #}
{% autoescape off %}
    {{ h1Tag }}
{% endautoescape %}
</body>
</html>

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Categories

  • 猫
  • Python
  • MySQL
  • Django
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • php
  • Practice
  • Virtualization
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • Router
  • Office
  • WordPress
  • SEO
  • English
  • Games
  • Recipes
  • living
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2022 Alaica Blog support@alaica.com