• Django的用户认证组件


    1.用户认证

      1.auth模块

    from  django.contrid import auth

      2.authenticate()

    提供了用户认证,验证用户名和密码是否正确,一般使用username   password两个关键字参数

      user_obj=auth.authenticate(username=user,password=pwd)
    如果认证信息有效,会返回一个  User  对象。authenticate()会在User 对象上设置一个属性标识那种认证后端认证了该用户,且该信息在后面的登录过程中是需要的。
    当我们试图登陆一个从数据库中直接取出来不经过authenticate()的User对象会报错的!!
    3.用户登录
    视图部分:
    
    
    def login(request):
        if request.method=="GET":
            return render(request,"index.html")
        else:
            user=request.POST.get("user")
            pwd=request.POST.get("pwd")
    
            user_obj=auth.authenticate(username=user,password=pwd)
            if user_obj:
                auth.login(request,user_obj)
                return  redirect("/books/")
            else:
                return redirect("/login/")
    
    
    
    模板部分:
    <!doctype html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title></title>
        <link rel="stylesheet" type="text/css" href="/static/css/styles.css">
        <!--[if IE]>
            <script src="http://libs.baidu.com/html5shiv/3.7/html5shiv.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="jq22-container" style="padding-top:100px">
            <div class="login-wrap">
                <div class="login-html">
                    <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">SIGN IN</label>
                    <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab"></label>
                    <div class="login-form">
                        <div class="sign-in-htm">
                            <form action="" method="post">
                                {% csrf_token %}
                                <div class="group">
                                <label for="user" class="label">请输入用户名</label>
                                <input id="user" type="text" class="input" name="user">
                                </div>
                                <div class="group">
                                    <label for="pass" class="label">请输入密码</label>
                                    <input id="pass" type="password" class="input" data-type="password" name="pwd">
                                </div>
                                <div class="group">
                                    <input id="check" type="checkbox" class="check" checked>
                                    <label for="check"><span class="icon"></span> Keep me Signed in</label>
                                </div>
                                <div class="group">
                                    <input type="submit" class="button" value="登录">
                                </div>
    
    
    
                                <div style="text-align: center;height: 200px;line-height: 200px">
                                    有账号请登录,没有账号请!<a href="/reg/" style="color: white">注册 </a>
                                </div>
                                <div class="hr"></div>
    
    
                            </form>
    
                        </div>
    
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
    </body>
    </html>
    
    

      4.用户注册视图部分:

    def reg(request):
        if request.method=="GET":
            return render(request,"reg.html")
        else:
            user=request.POST.get("user")
            pwd=request.POST.get("pwd")
            User.objects.create_user(username=user,password=pwd)
            return redirect("/login/")

    模板部分
    <!doctype html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title></title>
        <link rel="stylesheet" type="text/css" href="/static/css/styles.css">
        <!--[if IE]>
            <script src="http://libs.baidu.com/html5shiv/3.7/html5shiv.min.js"></script>
    
        <![endif]-->
        <style >
            .hr{
                margin-bottom: 30px;
            }
    
        </style>
    </head>
    <body>
        <div class="jq22-container" style="padding-top:100px">
            <div class="login-wrap">
                <div class="login-html">
                    <input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">SIGN UP
    </label>
    
                    <input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab"></label>
                    <div class="login-form">
                        <div class="sign-in-htm">
                            <form action="" method="post">
                                {% csrf_token %}
                                <div class="group">
                                <label for="user" class="label">请输入用户名</label>
                                <input id="user" type="text" class="input" name="user">
                                </div>
                                <div class="group">
                                    <label for="pass" class="label">请输入密码</label>
                                    <input id="pass" type="password" class="input" data-type="password" name="pwd">
                                </div>
                                <div class="group">
                                    <input id="check" type="checkbox" class="check" checked>
                                    <label for="check"><span class="icon"></span> Keep me Signed in</label>
                                </div>
                                <div class="group">
                                    <input type="submit" class="button" value="注册">
    
    
                                </div>
    {#
                                <div style="text-align: center;height: 200px;line-height: 200px">
                                    有账号请<a href="/login/" style="color: white"> 登录</a>,没有账号请注册!
                                </div>
    
    
    
                                <div class="hr" style="text-align: center">
    
                                </div>
    
    
                            </form>
    
    
    
                        </div>
    
    
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
    </body>
    </html>
      5.用户注销:
    视图:
    def logout(request):
        auth.logout(request)
        name=request.user.username
    
        return render(request,"reg.html",locals())
    

       6.修改密码

    使用 set_password() 来修改密码

    1
    2
    3
    user = User.objects.get(username='')
    user.set_password(password='')
    user.save 
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    @login_required
    def set_password(request):
        user = request.user
        state = None
        if request.method == 'POST':
            old_password = request.POST.get('old_password', '')
            new_password = request.POST.get('new_password', '')
            repeat_password = request.POST.get('repeat_password', '')
            if user.check_password(old_password):
                if not new_password:
                    state = 'empty'
                elif new_password != repeat_password:
                    state = 'repeat_error'
                else:
                    user.set_password(new_password)
                    user.save()
                    return redirect("/log_in/")
            else:
                state = 'password_error'
        content = {
            'user': user,
            'state': state,
        }
        return render(request, 'set_password.html', content)
  • 相关阅读:
    被隐藏的文件更改为可见
    Selenium WebDriver多层表单切换
    for循环
    Java课程设计二次大作业
    Java-DAO模式代码阅读及应用
    编辑器、编译器、文件、IDE等常见概念辨析
    树、二叉树和查找等知识点的总结
    二叉树的实现
    二叉树顺序结构和链式结构的相互转换
    使用k-近邻算法改进约会网站的配对效果
  • 原文地址:https://www.cnblogs.com/wqzn/p/9889948.html
Copyright © 2020-2023  润新知