本例中應用名稱爲cookiehtml
from django.db import modelspython
from django.db import models class UserInfo(models.Model): username = models.CharField(max_length=64) password = models.CharField(max_length=64)
數據庫遷移算法
python manage.py makemigrations
python manage.py migrate
給數據庫添加數據數據庫
from django.contrib import admin from django.urls import path from cookie import views urlpatterns = [ path('admin/', admin.site.urls), path('login/', views.login), path('index/', views.index), ]
from django.shortcuts import render, redirect, HttpResponse from cookie import models def login(request): ret = "" if request.method == "POST": user = request.POST.get("user") pwd = request.POST.get("pwd") # 查詢數據庫中的用戶名和密碼 user_obj = models.UserInfo.objects.filter(username=user, password=pwd).first() if user_obj: """ 若是驗證經過,則重定向至index並設置cookie """ obj = redirect("/index/") obj.set_cookie("is_login", True, 30) # cookie有效期爲30秒 obj.set_cookie("username", user_obj.username) return obj else: ret = "用戶名或密碼錯誤" return render(request, "login.html", {"ret": ret}) def index(request): is_login = request.COOKIES.get("is_login") username = request.COOKIES.get("username") if not is_login: return redirect("/login/") return render(request, "index.html", { "user": username })
登陸頁面 login.htmldjango
<body> <h3>用戶登陸頁面</h3> <form action="/login/" method="post"> {% csrf_token %} <p>用戶名:<input type="text" name="user"></p> <p>密碼:<input type="password" name="pwd"></p> <p><span>{{ ret }}</span></p> <input type="submit" value="登陸" class="sub_btn"> </form> </body>
首頁index.htmlcookie
<body> <h3>這是index頁面。你好 {{ user }}</h3> </body>
下來訪問測試一下session
點擊登陸app
能夠看到,已經按預想跳轉至index頁面函數
下來看看設置的cookiepost
再來看看,重定向訪問時攜帶的cookie值
python manage.py startapp session
設置settings.py
INSTALLED_APPS = [ ... 'cookie.apps.CookieConfig', 'session.apps.SessionConfig' ]
..... from session import views as session_views urlpatterns = [ ...... path('session_login', session_views.session_login), path('session_index', session_views.session_index), path('session_logout', session_views.session_logout), ]
from django.shortcuts import render, redirect from cookie import models # 因爲要用到數據,全部要吃cookie應用裏面導入 def session_login(request): ret = "" if request.method == "POST": user = request.POST.get("user") pwd = request.POST.get("pwd") user_obj = models.UserInfo.objects.filter(username=user, password=pwd).first() if user_obj: """ 若是驗證經過,則設置session,並返回session_index """ request.session["is_login"] = True request.session["username"] = user_obj.username obj = redirect("/session_index/") return obj else: ret = "用戶名或密碼錯誤" return render(request, "session_login.html", {"ret": ret})
def session_index(request): is_login = request.session.get("is_login") username = request.session.get("username") if not is_login: """若是沒有登陸則跳轉至登陸頁面""" return redirect("/session_login") return render(request, "session_index.html", {"user": username}) def session_logout(request): request.session.flush() return redirect("/session_login/")
session_index.html
<h3>基於session的用戶登陸</h3> <form action="/session_login/" method="post"> {% csrf_token %} <p>用戶名:<input type="text" name="user"></p> <p>密碼:<input type="password" name="pwd"></p> <p><span>{{ ret }}</span></p> <input type="submit" value="登陸" class="sub_btn"> </form>
session_index.html
<body> <h3>這是session_index頁面。</h3> <h3>你好 {{ user }}</h3> <a href="/session_logout/">註銷</a> </body>
下來測試
能夠看到數據庫中session表已經有數據了,session_key爲隨機生成的,爲請求時攜帶的cookie鍵,session_data爲
request.session["is_login"] = True
request.session["username"] = user_obj.username,通過某種算法加密後的數
再來看看頁面