Django session
阿新 • • 發佈:2017-06-11
ssi strong str 模板頁 pass .post from word ons
- Session登陸實例
session存在於客戶端,cookies存在於服務器
views:
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import render,render_to_response,HttpResponse,redirect from django.contrib import sessions # Create your views here. 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‘] = {‘username‘:user}#給本次會話者記錄下來 return redirect(‘/app02/index/‘)#重定向到目標頁面 else: return render_to_response(‘app02/login.html‘,{‘msg‘:‘用戶名或密碼錯誤‘})#前一個參數是模板頁面,後一個參數是變量return render_to_response(‘app02/login.html‘) def index(request): user_dict = request.session.get(‘is_login‘,None) if user_dict:#如果此時會話存在則可以進入這個頁面 return render_to_response(‘app02/index.html‘,{‘userinfo‘:user_dict[‘username‘]}) else:#否則重新登陸 return redirect(‘/app02/login/‘) def logout(request): #刪除sessiondel request.session[‘is_login‘] return redirect(‘/app02/login/‘)
具體配置,見:http://www.cnblogs.com/wupeiqi/articles/5246483.html
Django session