1. 程式人生 > >Auth模塊

Auth模塊

per cat mail ole django create meta put tro

一:Auth模塊簡介

二:Auth模塊常用方法

三:擴展默認的auth_user表

一:Auth模塊簡介

Auth模塊是Django自帶的用戶認證模塊:

我們在開發一個網站的時候,無可避免的需要設計實現網站的用戶系統。此時我們需要實現包括用戶註冊、用戶登錄、用戶認證、註銷、修改密碼等功能

Django內置了強大的用戶認證系統--auth,它默認使用 auth_user 表來存儲用戶數據。

鏈接數據庫後有自帶的auth_user

二:Auth模塊常用方法

技術分享圖片
from django.contrib import auth
from django.contrib.auth.decorators import
login_required def auth_test(request): if request.method==POST: name=request.POST.get(name) pwd=request.POST.get(pwd) #做認證 user=auth.authenticate(request,username=name,password=pwd) # user=models.User.objece.filter(username=name,password=pwd).first()
if user: # 做登錄 auth.login(request,user) return HttpResponse(登錄成功) else: return HttpResponse(登錄失敗) return render(request,index.html) def auth_test2(request): # print(request.user.username) # print(request.user.password)
# print(type(request.user)) print(request.user) return render(request,auth_test.html) def auth_logout(request): auth.logout(request) return HttpResponse(註銷成功) @login_required def auth_createuser(request): # UserInfo.objects.create() # user1=UserInfo.objects.create_superuser(username=‘lqz1‘,password=‘lqz12345‘,email=‘[email protected]‘) # user2=UserInfo.objects.create_user(username=‘lqz2‘,password=‘lqz12345‘,email=‘[email protected]‘) # user = auth.authenticate(request, username=‘lqz1‘, password=‘lqz12345‘) # if user: # print(‘1111‘) # else: # print(‘222‘) if request.user.is_authenticated(): print(登錄了,request.user) else: print(沒有登錄,request.user) # user=request.user # 校驗密碼 # ret=user.check_password(‘lqz12345‘) # print(ret) # 設置密碼 # print(user) # !!!!!!一定要save # user.set_password(‘lqz11111‘) # user.save() return HttpResponse(註冊成功)
views.py 技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{%  if request.user.is_authenticated %}

{% endif %}
</body>
</html>
auth_test.html 技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/static/jquery-3.3.1.js"></script>
    <title>Title</title>
</head>
<body>
<form action="" method="post">
    {% csrf_token %}

    <p>用戶名:<input type="text" name="name" id="name"></p>
    <p>密碼:<input type="password" name="pwd"></p>
    <input type="submit">
</form>
<button id="ajax">ajax</button>

</body>
<script>
    $("#ajax").click(function () {
        $.ajax({
        url:‘‘,
        type:post,
        //data:{‘name‘:$("#name").val(),‘csrfmiddlewaretoken‘:$(‘[name="csrfmiddlewaretoken"]‘).val()},
        data:{name:$("#name").val(),csrfmiddlewaretoken:{{ csrf_token }}},
        success:function (data) {
            console.log(data)

        }

    })
    })

</script>

</html>
index.html 技術分享圖片
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/static/jquery-3.3.1.js"></script>
    <title>Title</title>
</head>
<body>
<form action="" method="post">

{#    {% csrf_token %}#}
    <p>用戶名:<input type="text" name="name" id="name"></p>
    <input type="submit">
</form>
<button id="ajax">ajax</button>

</body>
<script>
    $("#ajax").click(function () {
        $.ajax({
        url:‘‘,
        type:post,
        //data:{‘name‘:$("#name").val(),‘csrfmiddlewaretoken‘:$(‘[name="csrfmiddlewaretoken"]‘).val()},
        data:{name:$("#name").val(),csrfmiddlewaretoken:{{ csrf_token }}},
        success:function (data) {
            console.log(data)

        }

    })
    })

</script>

</html>
index1.html

三:擴展默認的auth_user表

Auth模塊