login_session.html文件
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Apollo</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <script> </script> </head> <body> <div class="container"> <form action="{% url 'login_session' %}" method="post"> {% csrf_token %} <div class="col-md-6 col-md-offset-3"> <div class="input-group"> <span class="input-group-addon" id="sizing-addon2">账号</span> <input type="text" class="form-control" name="usr" placeholder="Username" aria-describedby="sizing-addon2"> </div> <br> <div class="input-group"> <span class="input-group-addon" id="sizing-addon2">密码</span> <input type="text" class="form-control" name="pwd" placeholder="Password" aria-describedby="sizing-addon2"> </div> <br> <button class="btn btn-default pull-right">提交</button> {{ error }} </div> </form> </div> </body> </html>
index_session.html文件
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta > <title>Apollo</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <script src="/static/jquery.js"></script> </head> <body> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <a class="navbar-brand" href="javascript:void(0);">Apollo</a> <ul class="nav navbar-nav"> <li class="active"><a href="javascript:void(0);">主页</a></li> <li><a href="javascript:void(0);">python</a></li> <li><a href="javascript:void(0);">html</a></li> <li><a href="javascript:void(0);">css</a></li> <li><a href="javascript:void(0);">javascript</a></li> </ul> <form class="navbar-form navbar-left" action="javascript:void(0);"> <input type="text" placeholder="请输入关键词" class="form-control"> <button type="submit"> <span class="glyphicon glyphicon-search"></span> </button> </form> <a class="btn navbar-btn btn-primary btn-sm navbar-right" href="javascript:void(0);">免费下载</a> <div class="navbar-right"> <ul class="nav navbar-nav"> <li class="navbar-text">你好 {{ usr }}</li> <li><a class="navbar-link" href="{% url 'logout_session' %}">退出</a></li> </ul> </div> </div> </nav> <div class="container"> </div> </body> </html>
urls.py文件
from django.contrib import admin from django.urls import path from coo import views from django.views.generic.base import RedirectView urlpatterns = [ path('admin/', admin.site.urls), path("favicon.ico", RedirectView.as_view(url='static/favicon.ico')), # session 实现 path('login_session/', views.login_session, name='login_session'), path('index_session/', views.index_session, name='index_session'), path('logout_session/', views.logout_session, name='logout_session'), ]
views.py文件
from django.shortcuts import render, redirect, HttpResponse, reverse from coo.models import UserInfo # Create your views here. # ############### SESSION的login_session页面 ############### def login_session(request): if request.method == 'GET': return render(request, 'login_session.html') else: # 获取网页form表单传来的usr usr = request.POST.get('usr') # 获取网页form表单传来的pwd pwd = request.POST.get('pwd') # 数据库查询该用户是否存在 usr_obj = UserInfo.objects.filter(usr=usr, pwd=pwd).first() print(0000000000000000000) # 信息存在,进行保存 if usr_obj: # 用户认证信息存储到session文件 request.session["username"] = usr request.session["is_active"] = True ''' if request.COOKIE.get("sessionid"): random_str=request.COOKIE.get("sessionid") 在django-seesion表中过滤session-key=random_str的记录进行update else: 1 生成一个随机字符串 23423hkjsf890234sd 2 向django-session表中插入记录 session-key session-data 23423hkjsf890234sd {"susername":"egon","slogin":True} 3 响应set_cookie: {"sessionid":23423hkjsf890234sd} ''' # 登录成功跳转到主页index_session return redirect("/index_session/") else: print(999999999999999) # 如果不存在,进行信息提示 error = 'USERNAME AND PASSWORD DO NOT MATCH' return render(request,'login_session.html',{'error':error}) # ############### SESSION的index_session页面 ############### def index_session(request): ''' request.session 1 request.COOKIE.get("sessionid") :23423hkjsf890234sd 2 在django-session表过滤session-key=23423hkjsf890234sd的记录 3 取过滤记录的session-data反序列化成数据字典:{"susername":"egon","slogin":True} ''' is_active = request.session.get("is_active") if not is_active: return redirect("/login_session/") username = request.session.get("username") return render(request, 'index_session.html', locals()) # ############### SESSION的logout_session ############### def logout_session(request): ''' 1 request.COOKIE.get("sessionid") :23423hkjsf890234sd 2 在django-session表过滤session-key=23423hkjsf890234sd的记录删除 3 response.delete_cookie("sessionid") ''' request.session.flush() return redirect("/login_session/")