Webpy Register Login __web

Source: Internet
Author: User

index.py

#coding =utf-8 Import Web import datetime from Web import form web.config.debug = False import sys reload (SYS) SYS.SETDEFAU Ltencoding ("Utf-8") urls= ('/', ' Index ', '/reg ', ' reg ', '/login ', ' login ', '/logout ', ' logout ', '/sh ') Owusers ', ' showusers ',) app=web.application (Urls,globals ()) render = Web.template.render (' Templates ', base= ' base ') se Ssion = web.session.Session (app, Web.session.DiskStore (' Sessions '), initializer={' login ': 0,}) db1=web.database (dbn= ' MySQL ', db= ' ideploy1 ', user= ' ideploy1 ', host= ' 10.13.38.43 ', pw= ' 123456 ', port=3306,) # # #首页 class I Ndex:def Get (self): #return Web.ctx.ip return Datetime.datetime.now (). Strftime ('%y%m%d%h%m%s%s ') ### Register Page class Reg:vusername= Form.regexp (R). { 3,20}$ ", ' username length is 3-20 bits ') Vpass = Form.regexp (r). {
    6,20}$ ", ' password length is 6-20 digits ') Vemail = Form.regexp (r". *@.* "," must be a valid e-mail address ") Regform=web.form.form ( Form.
Textbox (' username ', vusername,description=u ' username '),    Form. Password ("Password", vpass,description=u "password"), form. Password ("Password2", description=u "Confirm password"), form. button (U "Register Now", type= "submit", description= "submit"), validators = [form. Validator ("Two input passwords inconsistent", lambda i:i.password = = i.password2)] def get (self): return Render.reg (SE
        Lf.regform def POST (self): Formdata=web.input ();
        Username=web.net.websafe (Formdata.username) Password=web.net.websafe (Formdata.password) Regip=web.ctx.ip Regdate=datetime.datetime.now (). Strftime ('%y%m%d%h%m%s ') if not self.regForm.validates (): retur
                    n Render.reg (self.regform) #判断用户名是否存在: Else:if len (getuserbyusername (username)) >0: Return Render.reg (self.regform,u ' username already exists ') Else:ret=adduser (username,
                     password,regip,regdate) If ret==0: #return u ' registered successfully '   Raise Web.seeother ('/showusers ') else:return u ' registration failed ' # # #登陆页面 class Logi N:loginform=form. Form (form. Textbox (' username ', description=u ' username '), form. Password ("Password", description=u "password"), form.  button (U "Login Now", type= "submit", description= "Submit"), Def get (self): If logged (): Back ' you are
        Logged ' Else:return Render.login (self.loginform) def POST (self): Postdata=web.input () Username=web.net.websafe (Postdata.username) Password=web.net.websafe (Postdata.password) rslist=getUs
            Erbyusername (username) If Len (rslist) ==0:return render.login (Self.loginform, ' username not present ') Else: If Password==rslist[0].password:session.login=1 #session. Login
   222=1 return ' Landing success, welcome you: ' +username+ ': ' +str (session.login222) Else:             Return Render.login (Self.loginform, ' username and password mismatch ') # # #注销登陆页面 class Logout:def get (self):
        Session.login=0 Session.kill () return ' logoff success ' # # #显示用户列表 class Showusers:def get (self): If logged (): Userslist=getusers () return render.users (userslist) Else:retur


 

n ' I'm sorry you did not login do not have permission to view ' # # #判断用户是否登陆 def logged (): If Session.login==1:return True else:return False ################################################ database operations part begin ################################################## ########## #增加用户 def addUser (username,password,regdate,regip,isactive= ' 0 '): Ret=db1.insert (' T_user ', username =username, password=password,regdate=regdate,regip=regip,isactive=isactive) return ret #获取用户列表 def getusers (limit=10 000,orderby= ' RegDate '): Rs=db1.select (' T_user ', Limit=limit,order=orderby) rslist=[] for R in Rs:rslis
T.append (R) return rslist
#根据用户名查询用户信息 def getuserbyusername (username): rs=db1.select (' T_user ', where= ' username= ', ' +username+ ') rslist=[] For R in Rs:rslist.append (r) return rslist ################################################ number
 According to the library operation part end ############################################################ if __name__== "__main__": App.run ()


Base.html

$def with (page)


Users.htm

$def with (users)
<table>
<tr><td> username </td><td> password </td></tr>
$for i in users:
	<tr><td> $i .username</td><td> $i. password</td><td>$ i.regip</td><td> $i .regdate</td></tr>

</table>


Reg.htm

$def with (form,message= ')
<form method= "POST" action= '/reg ' >
		$:message
    $:form.render ()
</form>


Login.htm

$def with (form,message= ')
<form method= "POST" action= '/login ' >
		$:message
    $:form.render ()
</form>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.