Custom user login, use a custom table to hold user information, use session to maintain session connection, after closing the browser session expires, login can automatically jump to the original page:
Session Management: Http://www.cnblogs.com/dreamer-fish/p/5434962.html
#-*-coding:utf-8-*- fromDjango.shortcutsImportRender fromDjango.httpImportHttpresponse,httpresponseredirect fromDjango.core.urlresolversImportReverse fromDjango.contrib.authImportAuthenticate,login as auth_login,logout as Auth_logout fromAptest.formsImportHvform,testform,vmform,lusersform fromAptest.modelsImportlusersImport Time#Create your views here.GlobalUser_loggedin,login_url,index_urluser_loggedin='Guest' #Set the default user name that is not logged on as guestLogin_url ='.. /login' #set the login page URLIndex_url ='.. /' #Set the login success to jump to the homepage URL
#定义登录检查函数defLogin_req (url=login_url):defGetfun (func):defWrapper (Request,*args, * *kv):GlobalUser_loggedin#set User_loggedin to global, otherwise view will not get this valuerequest.session['Backurl'] = Request.path#gets the URL of the current page, and automatically jumps back to the page after the login is successful, none is obtained #Request.session.set_expiry (1800)UserInfo = Request.session.get ('S_username', None)#gets session, set to none if not obtained if notuserinfo:returnHttpresponseredirect (URL)#without login, jump to login page Else: User_loggedin=request.session['S_username']#Get the current login user name returnFunc (Request,*args, * *kv)returnwrapperreturngetfun#定义主页函数defIndex (Request):returnHttpResponse (U"Welcome to aa!")@login_req ()defAdd (Request): a=3b=55C=int (a) +Int (b) Err=[] ifRequest.method = ='POST': #Print request. post[' Hvname '],request. post[' HVIP '] PrintRequest.method Form=Hvform (Request. POST)ifForm.is_valid ():#determine if the input data is legitimate #Print Form #print form.cleaned_data[' name '],form.cleaned_data[' IP ']FC =Form.cleaned_dataPrintEcx'name'],fc['IP'] Else: Err.append (form.errors)#Output error message Else: Form=Hvform ()hour_offset=time.ctime () LS= Range (10) Context={'Hour_offset': Hour_offset,'ls': LS,'Err': Err,'User_loggedin': User_loggedin} returnRender (Request,'aptest/form.html', context)@login_req () #使用装饰函数, check whether you are logged in before logging indefTESTVW (Request): Err=[] ifRequest.method = ='POST': PrintRequest.method Form=Vmform (Request. POST)ifForm.is_valid ():#determine if the input data is legitimateFC =Form.cleaned_dataPrintfc['Hvname'],fc['name'],fc['CPU'] Else: Err.append (form.errors)#Output error message Else: Form=Vmform () context={'Err': Err,'User_loggedin': User_loggedin}returnRender (Request,'aptest/current_datetime.html', context) #定义用户登录函数, and determine whether to automatically jump to the previous page after logging in, or jump to the home pagedefLogin (Request):GlobalUser_loggedin errors_list=[] ifRequest.method = ='POST': Form=Lusersform (Request. POST)ifform.is_valid (): FC=Form.cleaned_data#print fc[' name '],fc[' password '] Try: UserO= Lusers.objects.get (name=fc['name']) ifUsero.password = = fc['Password']: request.session['S_username'] = Usero.name#set the user's sessionUser_loggedin = Usero.name#get user's username after login #print ' Login-get backurl: ', Request.session.get (' Backurl '), request.session[' S_username '] ifRequest.session.get ('Backurl') is notNone:#check if the URL before the jump existsRedirectURL = request.session['Backurl'] Else: RedirectURL= Index_url#The URL before the jump does not exist then jumps to the homepage returnHttpresponseredirect (RedirectURL)Else: Errors_list.append ('password is not correct!') exceptException:errors_list.append (fc['name'] +'doesnot Exists') Else: Errors_list.append (form.errors)#output field format error message Else: Form=Lusersform () context={'errors_list': Errors_list,'User_loggedin': User_loggedin}returnRender (Request,'aptest/login.html', context)
#定义注销函数defLogout (Request):Try: #Delete the session that saved the user name and the pre-jump URL delrequest.session['S_username'] delrequest.session['Backurl'] exceptKeyerror as E:Pass #skip to the original page after logging off #return Httpresponseredirect (request. Meta.get (' http_referer ', '/')) returnHttpresponseredirect ('.. /login')
Modify the session expires after closing the browser:
1. Add Session_expire_at_browser_close=true on the last line of settings.py
2. Empty the saved Session:delete from Django_session
3. Empty IE Cache
4. Re-test OK, use Print request.session.get_expire_at_browser_close () to see if the session expires after the browser is closed
Custom user Login, session hold, auto jump