Django進階-auth集成認證模塊

auth認證模塊是Django內置集成的一個用戶認證模塊。css

auth認證模塊方法

方法 釋義
auth.authenticate() 認證校驗
auth.login(request,user) 封裝認證了的user對象
auth.logout(request) 將session數據都刪除,且cookie也失效

auth認證模塊示例

from django.shortcuts import render,redirect
from django.contrib import auth
from django.contrib.auth.decorators import login_required

def login(request):
    if request.method == "POST":
        user = request.POST.get("username")
        pwd = request.POST.get("password")

        user = auth.authenticate(username=user, password=pwd)
        # auth認證校驗,若是校驗成功返回用戶名,不然返回空
        
        if user:
            auth.login(request, user)
            # 封裝認證了的user對象
            return redirect("index.html")
    return render(request, "login.html")

auth認證模塊裝飾器使用

裝飾器,未登陸認證時沒法訪問 index 默認跳轉到指定頁面,在setting中 配置LOGIN_URL = "跳轉的頁面名稱" 如:html

LOGIN_URL = "login.html"
@login_required
def index(request):
    print("登陸的用戶是:",request.user.username)
    return render(request,"index.html")

auth認證模塊實例

目錄架構

MyDjango
    APP
        html
            css
            images
            js
            static
                index.html
                login.html
        migrations
        views
            index.py
    MyDjango
        settings.py
        urls.py
        wsgi.py
    db.sqlite3
    manage.py

配置文件

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>wellcome</title>
</head>
<body>
{% csrf_token %}
    <h1>wellcome index web !!!</h1>
    <a href="login.html">退出</a>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="post" action="login.html">
    {% csrf_token %}
    用戶名:<input type="text" name = "username">
    密碼:  <input type="text" name = "password">
    <input type="submit">
</form>
</body>
</html>
index.py
from django.shortcuts import render,redirect,HttpResponse
from django.contrib import auth
from django.contrib.auth.decorators import login_required

def login(request):
    auth.logout(request)
    if request.method == "POST":
        user = request.POST.get("username")
        pwd = request.POST.get("password")
        print("用戶名:",user,"密碼:",pwd)
        user = auth.authenticate(username=user, password=pwd)
        # auth認證校驗,若是校驗成功返回用戶名,不然返回空

        if user:
            auth.login(request, user)
            # 封裝認證了的user對象
            return redirect("/index.html")
        else:
            return HttpResponse("登陸失敗,用戶或密碼錯誤!")
    return render(request, "login.html")

@login_required
def index(request):
    user = request.user.username
    print("用戶",user,"登陸成功!")
    return render(request, "index.html",{"user":user})
settings.py

TEMPLATESpython

'DIRS': [os.path.join(BASE_DIR, 'APP/html/static')]
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR,"APP/html/static"),)
STATIC_ROOT = 'APP/html'
LOGIN_URL = "login.html"
urls.py
from django.contrib import admin
from django.urls import path,re_path
from APP.views import index


urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^login.html$', index.login),
    re_path('^index.html$', index.index),
]

服務運行

生成數據表web

python manage.py makemigrations APP
python manage.py migrate

建立超級用戶用於登陸測試sql

python manage.py createsuperuser

服務運行django

python manage.py runserver
相關文章
相關標籤/搜索