Django的使用者認證元件

Bound_w發表於2018-11-01

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)

相關文章