django session的使用
阿新 • • 發佈:2019-01-02
# session的使用
def login(request):
if request.method == 'POST':
user = request.POST.get('username',None)
pwd = request.POST.get('password',None)
if user == 'alex' and pwd =='123':
request.session['is_login'] = {'user':user}
return redirect('/app02/index/')
else:
return render_to_response('app02/login.html',{'msg':'使用者名稱或密碼錯誤'})
def index(request):
user_dict = request.session.get('is_login',None)
if is_login:
return render_to_response('app02/index.html',{'username':user_dict['user']})
else:
return redirect('app02/login/')
def logout(request):
del request.session['is_login']
def login(request):
if request.method == 'POST':
user = request.POST.get('username',None)
pwd = request.POST.get('password',None)
if user == 'alex' and pwd =='123':
request.session['is_login'] = {'user':user}
return redirect('/app02/index/')
else:
return render_to_response('app02/login.html',{'msg':'使用者名稱或密碼錯誤'})
def index(request):
user_dict = request.session.get('is_login',None)
if is_login:
return render_to_response('app02/index.html',{'username':user_dict['user']})
else:
return redirect('app02/login/')
def logout(request):
del request.session['is_login']
return redirect('/app02/login/')
session的過期時間,在Django的 settings.py裡面設定;SESSION_COOKIE_AGE = 10 ,表示session的生命週期是 10 s,如果你的瀏覽器不支援COOKIE,那麼你的session是用不了的