1 设置cookie
本例中应用名称为cookie
模型model
from django.db import models
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
给数据库添加数据
路由urls
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.html
<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.html
<body> <h3>这是index页面。你好 {{ user }}</h3> </body>
下来访问测试一下
点击登录
可以看到,已经按预想跳转至index页面
下来看看设置的cookie
再来看看,重定向访问时携带的cookie值
2 设置session
创建应用
python manage.py startapp session
注册应用
设置settings.py
INSTALLED_APPS = [ ... ‘cookie.apps.CookieConfig‘, ‘session.apps.SessionConfig‘ ]
路由urls
..... 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"] = Truerequest.session["username"] = user_obj.username,经过某种算法加密后的数
再来看看页面
原文地址:https://www.cnblogs.com/Sunzz/p/10573246.html
时间: 2024-10-17 06:35:24