标签:自定义 img 不能 pos 并保存 就会 ges 访问 无法
一.为什么需要cookie?三.Cookie的单独使用
from django.shortcuts import render,redirect
# Create your views here.
def login(request):
if request.method == ‘POST‘:
if request.POST.get(‘username‘) == ‘admin‘ and request.POST.get(‘pwd‘) == ‘admin‘:
ret = redirect(‘/mainmenu/‘)
ret.set_cookie(‘username‘,request.POST.get(‘username‘))
return ret
else:
return render(request, ‘login.html‘)
else:
return render(request, ‘login.html‘)
def mainmenu(request):
print(‘cookie:‘,request.COOKIES)
print(‘sessionname:‘,request.session.get(‘username‘))
print(‘sessionpwd:‘,request.session.get(‘pwd‘))
if request.COOKIES.get(‘username‘,None):
return render(request,‘mainmenu.html‘)
else:
return redirect(‘/login/‘)
四.Cookie及Session的结合应用
from django.shortcuts import render,redirect
# Create your views here.
def login(request):
if request.method == ‘POST‘:
if request.POST.get(‘username‘) == ‘admin‘ and request.POST.get(‘pwd‘) == ‘admin‘:
request.session[‘username‘] = request.POST.get(‘username‘)
request.session[‘pwd‘] = request.POST.get(‘pwd‘)
return redirect(‘/mainmenu/‘)
else:
return render(request, ‘login.html‘)
else:
return render(request, ‘login.html‘)
def mainmenu(request):
print(‘cookie:‘,request.COOKIES)
print(‘sessionname:‘,request.session.get(‘username‘))
print(‘sessionpwd:‘,request.session.get(‘pwd‘))
if request.session.get(‘username‘,None):
return render(request,‘mainmenu.html‘)
else:
return redirect(‘/login/‘)
五.欢迎关注笔者公众号
标签:自定义 img 不能 pos 并保存 就会 ges 访问 无法
原文地址:http://blog.51cto.com/10836356/2336650