Django框架(三)

0627内容:

XSS攻击:

"""djangoxss URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r‘^$‘, views.home, name=‘home‘)
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r‘^$‘, Home.as_view(), name=‘home‘)
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r‘^blog/‘, include(‘blog.urls‘))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^comment/‘, views.comment),    #输入内容
    url(r‘^index/‘, views.index),    #查看内容
    url(r‘^test/‘, views.test),    #测试页
]

urls.py

from django.shortcuts import render
from django.utils.safestring import mark_safe
# Create your views here.
msg = []
def comment(request):
    if request.method == "GET":
        return render(request,‘comment.html‘)
    else:
        v = request.POST.get(‘content‘)
        if "script" in v:
            return render(request,‘comment.html‘,{‘error‘:‘小比崽子还黑我‘})
        else:
            msg.append(v)
            return render(request,‘comment.html‘)

def index(request):
    return render(request,‘index.html‘,{‘msg‘:msg})

def test(request):
    temp = "<a href=‘http://www.baidu.com‘>百度</a>"
    newtemp = mark_safe(temp)
    # return render(request,‘test.html‘,{‘temp‘:temp})
    return render(request,‘test.html‘,{‘temp‘:newtemp})

views.py

template文件(html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<form method="POST" action="/comment/">
    <input type="text" name="content" />
    <input type="submit" value="提交" />{{ error }}

</form>

</body>
</html>

comment.html 提交内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <h1>评论</h1>
    {% for item in msg %}
        <div>{{ item|safe }}</div> #加上safe代表标记提交的数据时安全的
    {% endfor %}
</body>
</html>

index.html 查看comment提交的内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    {{ temp|safe }}

</body>
</html>

test.html 测试页

xss总结:

XSS注入攻击(跨站脚本攻击)
    for(var i=0,j=0;i<999,j<9999;i++,j++)
    {
      window.alert("");
    }

    <html>
    <head>text</head>
    <body>
    <form>
    <input type="text" name="text">
    </form>
    </body>
    </html>
        <script>
        alert(‘sb‘)
        </script>

<script>
获取本地cookie,将cookie发送至www.xxx.com(黑客的网址,把本地的隐私cookie发送给黑客)
</script>

    <h1>评论</h1>
    {% for item in msg %}
        <div>{{ item|safe }}</div>
    {% endfor %}
    写上safe会出问题
    safe是别人能随便添加或者随便写的,一定要对特殊字符做过滤
<script>alert(‘sb‘)</script> 会弹出一个框,如果是循环会撑爆浏览器

标记某个字符串或者标签是安全的:
两种方式:
    1.前端页面的字符串{{ temp|safe}}加上 |safe
    2.from django.utils.safestring import mark_safe
      def test(request):
      temp = "<a href=‘http://www.baidu.com‘>百度</a>"
      newtemp = mark_safe(temp)
      # return render(request,‘test.html‘,{‘temp‘:temp})
    return render(request,‘test.html‘,{‘temp‘:newtemp})
xss攻击:
慎用safe和mark_safe
非要用,一定要在提交数据时过滤关键字

时间: 2024-08-27 07:47:11

Django框架(三)的相关文章

django框架&lt;三&gt;

一.ORM操作  1.django orm创建数据库的方法 (1)指定连接pymysql(python3.x),先配置__init__.py import pymysql pymysql.install_as_MySQLdb() (2).配置连接mysql文件信息 settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'django_orm', #你的数据库名称 'USER': '

Django框架目录索引

Django框架目录索引 自定义Web框架与jinja2模板 HTTP协议详细介绍 cookie和session Django框架 (一) 环境配置及简单使用 Django框架(二) MTV模型简介 Django框架(三) url反向解析与csrf-token设置 Django框架(四) Django之模板语法 Django框架(五) Django之模板继承 Django框架 (六) Django模型 Django框架(七) Django之ORM数据库操作 Django框架(八) Django之

第三百一十六节,Django框架,中间件

第三百一十六节,Django框架,中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的settings模块中,有一个 MIDDLEWARE变量,其中每一个元素就是一个中间件(也就是一个中间件模块的一个类),如下. settings模块中 #中间件 MIDDLEWARE = [ 'django.middleware.security.Securit

第三百二十一节,Django框架,发送邮件

第三百二十一节,Django框架,发送邮件 全局配置settings.py EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' #发送邮件引擎 EMAIL_USE_TLS = False #是否以https方式 EMAIL_HOST = 'smtp.163.com' #邮件smtp服务器 EMAIL_PORT = 25 #端口 EMAIL_HOST_USER = '[email protected]' #发件人 EMAIL_

第三百零五节,Django框架,Views(视图函数),也就是逻辑处理函数里的各种方法与属性

Django框架,Views(视图函数),也就是逻辑处理函数里的各种方法与属性 Views(视图函数)逻辑处理,最终是围绕着两个对象实现的 http请求中产生两个核心对象: http请求:HttpRequest对象 http响应:HttpResponse对象 所在位置:django.http 之前我们用到的参数request就是HttpRequest     HttpRequest对象 逻辑处理函数的第一个形式参数,接收到的就是HttpRequest对象,这个对象里封装着用户的各种请求信息,通过

Django框架之第三篇模板语法

Django框架之第三篇模板语法(重要!!!) 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: 在 Django 模板中遍历复杂数据结构的关键是句点字符  .(也就是点) views.py def index(request): name = "hello haiyan" i = 200 l = [11,22,33,44,55] d = {"name":&quo

第三百零三节,Django框架介绍

Django框架介绍 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的软件设计模式,即模型M,视图V和控制器C.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是CMS(内容管理系统)软件.并于2005年7月在BSD许可证下发布. 这套框架是以比利时的吉普赛爵士吉他手Django Reinhardt来命名的. Django框架,流程图

Python学习(三) Django框架简单搭建

为了快速学习Python进行Web的开发,所以我不准备从Python的基础学起,直接从Django框架入手,边学框架边学Python的基础知识. 下面就开始Django的快速开发之旅吧. 关于Django框架1.8的英文文档在其官网上都有,地址 https://docs.djangoproject.com/en/1.8/intro/ Python的开发手册地址:https://pythonspot.com/getting-started/ https://docs.djangoproject.c

第三百零四节,Django框架,urls.py模块——

Django框架,模块 一.urls.py模块 这个模块是配置路由映射的模块,当用户访问一个url地址时,通过这个路由映射模块,映射给对应的逻辑处理函数 urlpatterns等于的一个列表,列表里的一个元素就是一条路由映射 urlpatterns路由映射配置方式 参数说明: 一个正则表达式字符串一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串可选的要传递给视图函数的默认参数(字典形式)一个可选的name参数 urlpatterns = [ url(正则表达式, 映射函数,参数[