首页 > Web开发 > 详细

Django学习路27_HTML转义

时间:2020-05-16 10:30:47      阅读:107      评论:0      收藏:0      [点我收藏+]
谨慎使用

自动渲染语法
{{code|safe}}
urls.py 中添加对应的函数

url(rgetcode,views.getcode)
在 views.py 中添加

def getcode(request):
    code = "<h2> HTML 转义示例 </h2>"
    context_code = {
        code:code
    }
    return render(request,getcode.html,context=context_code)
getcode.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Getcode 示例</title>
</head>
<body>
<p>
    {{ code|safe }}
</p>
</body>
</html>

技术分享图片


网站注入 js ,除非数据绝对安全,否则不要使用 
{{code|safe}}
在 根目录下,创建静态文件夹, static

建立 JavaScript 文件
添加内容
alert("网站被攻陷~"); 
在 views.py 中添加

def getcode(request):
    code = """
    <h2> HTML 转义示例 </h2>
    
    <script type="text/javascript">
        alert("网站被攻陷了~");
    </script>
    """

    context_code = {
        code:code
    }
    return render(request,getcode.html,context=context_code)
在 html 数据中添加

{{code|safe}} 

code 为传递过来的参数

里面有外界注入的 js 代码

技术分享图片 


除非数据绝对安全,不然不要使用 {{ xxx|safe}}

 

进行自动渲染
autoescape off
不进行自动渲染
autoescape on


<body>
    {% autoescape on %}
        {{ code }}
    {% endautoescape %}
</body>

 

技术分享图片


 

2020-05-16

Django学习路27_HTML转义

原文:https://www.cnblogs.com/hany-postq473111315/p/12898638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!