url.py文件:
1 from django.contrib import admin 2 from django.urls import path 3 from boo import views 4 5 urlpatterns = [ 6 path('admin/', admin.site.urls), 7 8 path('login/', views.login),#登录 9 path('index/', views.index), 10 path('home/', views.home) 11 12 ]
views.py文件: 登录和成功跳转到idex页面
1 from django.shortcuts import render, redirect 2 from app01 import models 3 # Create your views here. 4 5 6 def login(request): 7 if request.method == "POST": 8 username = request.POST.get("username") #获取数据的用户名 9 pwd = request.POST.get("password") #获取数据的密码 10 user = models.UserInfo.objects.filter(name=username, password=pwd).first() # 和userinfo表中的数据进行 判断 11 if user: #如果正确 12 # 登陆成功 13 request.session["user_id"] = user.id #在session中记录 user.id 14 #request.session["name"] = user.name 如果有多个字段需要存放在session中 就很麻烦 所以只在session中记录id 在其他页面通过id进行查询 15 return redirect("/index/") 16 17 return render(request, "login.html") 18 19 20 def index(request): 21 user_id = request.session.get("user_id") # 22 #name = request.session.get("name") 从session中获取name 如果要获取多个值 就要写多个 23 user_obj = models.UserInfo.objects.filter(id=user_id).first() #所以通过id找到这个对象 24 #return render(request, "index.html", {"name": name}) 25 return render(request, "index.html", {"obj": user_obj})
login.html文件:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <form action="" method="post"> 10 {% csrf_token %} 11 12 <input type="text" name="username"> 13 <input type="password" name="password"> 14 <input type="submit"> 15 </form> 16 </body> 17 </html>
index.py文件:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <h1>这是index页面!</h1> 10 <!-- 通过后端传过来的obj 获取name--> 11 <h1>{{ obj.name }}</h1> 12 <h2>{{ obj.password }}</h2> 13 </body> 14 </html>
如果有多个页面都要判断用户是否登录 在views.py文件中要重复很多工作,所以把这个操作放到中间件中去执行
mymiddleware.py文件:
1 from django.utils.deprecation import MiddlewareMixin 2 from app01 import models 3 4 class MyAuth(MiddlewareMixin): 5 """ 6 中间件有五种方法: 7 process_request 8 process_view 9 process_exception 10 process_template_response 11 process_response 12 """ 13 14 def process_request(self, request): 15 print("一声叹息!!!") 16 user_id = request.session.get("user_id") #获取到session记录的user_id 17 if user_id: 18 # 登录的用户你要把request.user赋值为登陆的用户对象 19 user_obj = models.UserInfo.objects.filter(id=user_id).first() #通过user_id获取对象信息 20 request.user = user_obj #赋值 21 else: 22 # 没有登录的用户要把request.user赋值为匿名用户 23 request.user = {"name": "", "password": ""}
setting.py文件中添加: 在MIDDLEWARE中添加 :'mymiddleware.MyAuth',
1 MIDDLEWARE = [ 2 'django.middleware.security.SecurityMiddleware', 3 'django.contrib.sessions.middleware.SessionMiddleware', 4 'django.middleware.common.CommonMiddleware', 5 'django.middleware.csrf.CsrfViewMiddleware', 6 'django.contrib.auth.middleware.AuthenticationMiddleware', 7 'django.contrib.messages.middleware.MessageMiddleware', 8 'django.middleware.clickjacking.XFrameOptionsMiddleware', 9 'mymiddleware.MyAuth',
views.py文件:
1 def home(request): 2 print("这是在视图:home") 3 print(request.user) 4 user_id = request.session.get("user_id") #多个页面都要判断是否登录状态 操作同上 5 user_obj = models.UserInfo.objects.filter(id=user_id).first() 6 return render(request, "home.html") #对所有视图做统一操作 可以把它放到中间件中执行
home.html文件:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 9 <h1>这是home页面!</h1> 10 {% if request.user.name %} <!--如果request.user.name 有值--> 11 <p>这是登录的用户</p> 12 {% else %} 13 <p>这是匿名用户</p> 14 {% endif %} 15 </body> 16 </html>
(具体学习视频 :博客项目一 day77 :auth模块功能梳理)
标签:name,登录,request,django,session,user,id,页面 From: https://www.cnblogs.com/zs0621/p/16736541.html