栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Django项目web开发 tamlates的 html学习——safe模式autoescape实现转义

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django项目web开发 tamlates的 html学习——safe模式autoescape实现转义

html的安全转义,将传入的包含Html格式的文件显示出来!!!此方式有很大的风险

将code传递到html中, 并实现浏览器显示该html标签,可调用js实现网站前端被攻击。

def get_students(request):
    students=Student.objects.all()
    # 测试模板for循环,传入空字典,时显示empty
    # students=Student.objects.filter(s_name='aaa')
    # 直接往上下文中传入字典
    code="""
        你是谁?
        

你是谁?

""" student_dict={ 'class':'1班', 'name':"Mei", 'age':'20岁', 'code':code, } data={ 'students':students, 'student_dict':student_dict, 's_var':5, } return render(request,'studentlist.html',context=data)
    {{ student_dict.code }}
    

这是使用后的{{ student_dict.code|safe }}


{# 另外一种方式实现转义 on关闭,off是转义 #} {% autoescape off %} {{ student_dict.code }} {% endautoescape %}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号