cookie和session
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cookie和session相关的知识,希望对你有一定的参考价值。
注意:session默认是存储在sqlit里面
def login(request):
if request.method=="POST":
username=request.POST.get(‘user‘)
password=request.POST.get(‘pwd‘)
print(username,password)
if username==‘alex‘ and password==‘123‘:
response=redirect(‘/app01/index/‘)
response.set_cookie(‘username‘,username,max_age=5,path=‘/‘)
response.set_cookie(‘password‘,password,max_age=5,path=‘/‘)
return response
return render(request,‘login.html‘)
def index(request):
user=request.COOKIES.get(‘username‘)
pwd=request.COOKIES.get(‘password‘)
print(‘ddddddddcookie‘,user,pwd)
if user and pwd:
return render(request,‘index.html‘)
else:
return redirect(‘/app01/login/‘)
def auth(func):
def neibu(request,*args,**kwargs):
username = request.session.get(‘username‘)
password = request.session.get(‘password‘)
print(‘session‘, username, password)
if username and password:
return func(request,*args,**kwargs)
else:
return redirect(‘/app01/reg/‘)
return neibu
def reg(request):
print(‘请求体‘,request.body)
print(‘请求头‘,request.META)
if request.method=="POST":
username=request.POST.get(‘user‘)
password=request.POST.get(‘pwd‘)
print(username,password)
if username==‘alex‘ and password==‘123‘:
request.session[‘username‘]=username
request.session[‘password‘]=password
print(‘运行‘)
return redirect(‘/app01/tp2/‘)
return render(request,‘reg.html‘)
@auth
def tp2(request):
return render(request,‘tp2.html‘)
以上是关于cookie和session的主要内容,如果未能解决你的问题,请参考以下文章