• Django的认证系统


    auth认证
    在views.py文件中导入模块from django.contrib import auth

    1. 注册超级用户
    方式一:Django自带的多字段数据表
    python manage.py migrate #迁移数据库db.sqlite3,产生一个多字段的数据表
    python manage.py createsuperuser #输入命令 回车 会出现各个字段,进行添加即可
    方式二:在相应app中的models.py文件中,在Django自带多字段表基础上增添自定义的字段
    例如:
    (1)在models.py文件中写入
    from django.db import models
    from django.contrib.auth.models import AbstractUser
    class UserInfo(AbstractUser):
    phone = models.CharField(max_length=11)
    (2)执行数据库迁移,如果直接进行迁移命令会报错,我们要在settings.py文件
    中添加AUTH_USER_MODEL = "app01.UserInfo"配置,告诉Django引用自定义的表名,
    如果不加Django会默认自己的那张表
      (也就是说,按上面的方式扩展了内置的auth_user表之后,
       一定要在settings.py中告诉Django,我现在使用我新定义的UserInfo表来做用户认证。
      写法如下:
      引用Django自带的User表,继承使用时需要设置
      AUTH_USER_MODEL = "app名.自定义表名"
      再次注意
      一旦我们指定了新的认证系统所使用的表,我们就需要重新在数据库中创建该表,
      而不能继续使用原来默认的auth_user表了。)
      创建完成,得到在Django自带多字段表基础上增添了phone的字段数据表,在pycharm上添加phone数据

    2. 认证用户
    Django提供了登录认证的方法,省去了查找数据库语句,利用authenticate方法,即:
    from django.contrib.auth import AbstractUser
    obj = auth.authenticate(request,username=username,password=pwd)
    username和pwd是从前端传来的数据,它们与数据库中的username和password进行匹配
    如果和Django中的superuser数据库匹配正确,则返回数据表中该用户的对象,
    如果返回None,则说明匹配不成功,我们可以根据匹配的结果进行判断来执行接下来的代码

    3. 保存登录状态
    from django.contrib.auth import AbstractUser
    auth.login(request,obj)
    该函数接受一个HttpRequest对象,以及一个经过认证的User对象。

    该函数实现一个用户登录的功能。它本质上会在后端为该用户生成相关session数据。

    代码如下:

    from django.contrib.auth import authenticate, login
       
    def my_view(request):
      username = request.POST['username']
      password = request.POST['password']
      user = authenticate(request, username=username, password=password)
      if user is not None:
        login(request, user)
        # Redirect to a success page.
        ...
      else:
        # Return an 'invalid login' error message.
        ...
    认证及保存登录状态

    另一种写法:

    from django.shortcuts import render,HttpResponse,redirect
    from django.contrib import auth
    from django.contrib.auth.decorators import login_required
    
    def login(request):
        errmsg = ''
        if request.method == 'POST':
            username = request.POST.get('username')
            pwd = request.POST.get('pwd')
            obj = auth.authenticate(request,username=username,password=pwd)
            print(obj,type(obj))
            if obj:
                auth.login(request,obj)
                return render(request,'index.html')
            else:
                errmsg = '用户名或密码错误'
        return render(request,'login.html',{'errmsg':errmsg})
    
    def index(request):
        return render(request,'index.html')
    认证及登录保存
  • 相关阅读:
    Python与机器视觉(x)图像修复
    git push代码到远程新分支
    1024节日快乐~~~~
    【Python-GPU】GPU数据科学加速包——RAPIDS
    Echarts漂亮水滴图
    【深度学习】三维点云数据集总结
    poj 3273 Monthly Expense
    poj 3150 Cellular Automaton
    poj 3101 Astronomy
    hdu 4282 A very hard mathematic problem
  • 原文地址:https://www.cnblogs.com/zhaosijia/p/9688646.html
Copyright © 2020-2023  润新知