zoukankan      html  css  js  c++  java
  • Django笔记08

    Django中间件

      中间件类似于django的门卫,数据在进入和离开时都需要经过中间件

      能做全局访问的频率限制,身份校验,黑名单,白名单等全局的限制

      用法:新建一个文件夹,文件夹中新建一个py文件

           写的类必须继承MiddlewareMixin

         from django.utils.deprecation import MiddlewareMixin

    django允许用户自定义中间件,并且保留给用户五个可以自定义的方法
    process_request(self,request) 请求来时执行,这时返回一个HttpResponse对象,后续的中间件都不会走,直接同级返回
    process_response(self,request,response) 响应返回时执行
    process_view(self,request,callback,callback_args,callback_kwargs) 路由匹配成功执行视图函数之前
    process_exception(self,request,exception) 视图函数报错执行
    process_template_response(self,request,response)

    CSRF跨站请求伪造

      form表单中加上:{% csrf_token %}

      ajax使用csrf校验:data:{"csrfmiddlewaretoken":$("[name=csrfmiddlewaretoken]").val()}

      局部使用与局部禁用csrf

      导入 from django.views.decorators.csrf import csrf_exempt,csrf_protect

    FBV
    局部禁用 @csrf_exempt
    def index(request):   pass
    局部使用
    @csrf_protect
    def index(self):
      pass
    CBV
    from django.utils.decorators import method_decorator
    form django.views.decorators.csrf import csrf_exempt,csrf_protect
    @method_decorator(csrf_exempt,name="dispatch") 给类加上装饰器
    @method_decorator(csrf_exempt)给dispathch函数加上装饰器

    https://www.cnblogs.com/liuqingzheng/articles/9509739.html

    https://www.cnblogs.com/Dominic-Ji/p/9229509.html?tdsourcetag=s_pctim_aiomsg

    Auth认证模块

      auth_user 表默认创建

      创建用户不能手动在表中插入,密码是加密的

    auth认证
      from django.contrib import auth
      user = auth.authenticate(request,username=username,password=password)
    login(HttpRequest,user)
      该函数接受一个HttpRequest对象和一个经过认证的User对象
      该函数实现用户登录功能,本质上会在后端为该用户生成相关session数据
    注销
      auth.logout(reuqest)
      等价于删除session数据request.session.flush()
    装饰器校验是否登录及跳转
      from django.contrib.auth.decorators import login_required
      @login_required(跳转地址)
      def my_view(request):
        pass
      可以在配置文件中指定auth校验登录不合法统一跳转的路径
      LOGIN_UTL = '/login/' 既可以局部配置,也可以全局配置
    创建用户
      from django.contrib.auth.models import User
      User.objects.createuser() 创建普通用户
      User.objects.creqtesuperuser() 创建超级用户
    判断是否通过认证
      request.user.is_authenticated()
    校验密码,修改密码
      request.user.check_password(password)检查密码是否正确
      request.user.set_password(password="xxx") 修改密码
      reuqest.user.save() 保存设置
    User对象属性
      is_staff:用户是否拥有网站的管理权限
      is_active:是否允许用户登录,设置False,可以在不删除用户的前提下禁止用户登录
    扩展默认auth_user表
      第一种:新建一张表与auth_user表一对一外键关联
      第二种:
        from django.contrib.auth.models import AbstractUser
        class UserInfo(AbstractUser):
          phone = models.CharField(max_length=32)
        在settings配置文件中指定使用UserInfo表
        AUTH_USER_MODEL = "app01.UserInfo"

    https://www.cnblogs.com/liuqingzheng/articles/9628105.html

  • 相关阅读:
    The Future of Middleware and the BizTalk Roadmap
    FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
    Strips illegal Xml characters
    luogu P2280 激光炸弹(二维前缀和)
    luogu P2704 炮兵阵地(经典状态压缩DP)
    SP1716 GSS3 Can you answer these queries III (线段树维护最大连续子段和)
    二分图判定、匹配问题
    C++语法综合 | 基于char*设计一个字符串类MyString
    luogu P1044 火车进出栈问题(Catalan数)
    C++设计模式 | 三种设计模式基础
  • 原文地址:https://www.cnblogs.com/LinChengcheng/p/10756958.html
Copyright © 2011-2022 走看看