session小记
Posted gaota1996
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session小记相关的知识,希望对你有一定的参考价值。
views.py
from django.shortcuts import render, redirect #通过用户登录演示session def session1(request): uname = request.session.get(‘myname‘, ‘未登录‘) # 没有这个值默认None # uname = None context = {‘uname‘: uname} return render(request, ‘booktest/session1.html‘, context) def session2(request): return render(request, ‘booktest/session2.html‘) def session2_handel(request): uname = request.POST[‘uname‘] request.session[‘myname‘] = uname return redirect(‘/booktest/session1/‘) def logout(request): # 删除session del request.session[‘myname‘] return redirect(‘/booktest/session1/‘)
urls.py
from booktest import views from django.urls import path urlpatterns = [ path(‘session1/‘, views.session1), path(‘session2/‘, views.session2), path(‘session2_handel/‘, views.session2_handel), path(‘logout/‘, views.logout) ]
session1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 你好:{{ uname }}<br> <hr> <a href="/booktest/session2/">登录</a> <hr> <a href="/booktest/logout/">退出</a> </body> </html>
session2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/booktest/session2_handel/" method="post"> <input type="text" name="uname"> <input type="submit" name="登录"> </form> </body> </html>
以上是关于session小记的主要内容,如果未能解决你的问题,请参考以下文章
C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段