python - Google app engine - Bugs in the code -
my code this:
import webapp2 import re form = """<html> <form method=post> <input name="username"><div id=div>%s</div><br> <input name="password"><div id=div>%s</div><br> <input name="verify"><div id=div>%s</div><br> <input name="email"><div id=div>%s</div> <input type="submit"> </form> </html>""" class mainpage(webapp2.requesthandler): def get(self): self.response.headers['content-type'] = 'text/html' self.response.write(form % ('','','','')) def reply(self,user,password,verify,email): if not user , password , verify , email: self.redirect('/broski') else: self.response.write(form % (user,password,verify,email)) def post(self): self.usererror = '' if not re.compile(r"^[a-za-z0-9_-]{3,20}$").match(self.request.get('username')): self.usererror = "that's not valid username." self.passworderror = '' if not re.compile(r"^.{3,20}$").match(self.request.get('password')): self.passworderror = "that wasn't valid password." self.verifyerror = '' if self.request.get('verify') != self.request.get('password') , self.request.get('password') != '': self.verifyerror = "your passwords didn't match." self.emailerror = '' if not re.compile(r"^[\s]+@[\s]+\.[\s]+$").match(self.request.get('email')): self.emailerror = "that's not valid email." self.reply(usererror,passworderror,verifyerror,emailerror) class thankyo(webapp2.requesthandler): def get(self): self.response.write('welcome, ' + self.request.get('username')+'!') app = webapp2.wsgiapplication([('/', mainpage),('/broski', thankyo)], debug=true)
it literally first time writing code gae(and im new python..) larger 10 lines and, ofcourse, broke. found several bugs absolutely on website still doesnt load. regarding code highly appreciated. update: updated little bit more(changed inheritance of thankyo) , form loads, post data ofcourse crashes...
class thankyo(mainpage):
should be
class thankyo(mainpage):
(note capital p
).
i found looking @ error messages left in terminal:
% dev_appser file "/home/unutbu/pysrc/project/main.py", line 38, in <module> ... class thankyo(mainpage): nameerror: name 'mainpage' not defined
also change
self.reply(usererror,passworderror,verifyerror,emailerror)
to
self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror)
i found again looking @ terminal error message:
file "/home/unutbu/pysrc/project/main.py", line 39, in post self.reply(usererror, passworderror, verifyerror, emailerror) nameerror: global name 'usererror' not defined
how see error messages (from terminal):
there more 1 way start gae server. way opening terminal , running
dev_appserver.py /path/to/project/
the terminal spews useful error messages.
the last problem bit more complex. sending error messages reply
method:
self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror)
but in reply
method definition call them user
, password
, verify
, email
. causes bit of cognitive dissonance. let's rename them usererror
, passworderror
, verifyerror
, emailerror
:
def reply(self, usererror, passworderror, verifyerror, emailerror):
now check there no errors use:
if not any(usererror , passworderror , verifyerror , emailerror):
but page want redirect needs name of user! if redirect /broksi
:
self.redirect('/broski')
then thankyo
handler has no idea username is. self.request.get('username')
come blank (return empty string). need pass along username broski
:
import urllib self.redirect('/broski?' + urllib.urlencode( {'username' : self.username}))
so code ends looking this:
import urllib import webapp2 import re form = """<html> <form method=post> <input name="username"><div id=div>%s</div><br> <input name="password"><div id=div>%s</div><br> <input name="verify"><div id=div>%s</div><br> <input name="email"><div id=div>%s</div> <input type="submit"> </form> </html>""" class mainpage(webapp2.requesthandler): def get(self): self.response.headers['content-type'] = 'text/html' self.response.write(form % ('', '', '', '')) def reply(self, usererror, passworderror, verifyerror, emailerror): if not any(usererror , passworderror , verifyerror , emailerror): self.redirect('/broski?' + urllib.urlencode( {'username' : self.username})) else: self.response.write(form % (user, password, verify, email)) def post(self): self.username = self.request.get('username') self.password = self.request.get('password') self.verify = self.request.get('verify') self.email = self.request.get('email') self.usererror = '' if not re.compile(r"^[a-za-z0-9_-]{3,20}$").match(self.username): self.usererror = "that's not valid username." self.passworderror = '' if not re.compile(r"^.{3,20}$").match(self.password): self.passworderror = "that wasn't valid password." self.verifyerror = '' if self.verify != self.password , self.password != '': self.verifyerror = "your passwords didn't match." self.emailerror = '' if not re.compile(r"^[\s]+@[\s]+\.[\s]+$").match(self.email): self.emailerror = "that's not valid email." self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror) class thankyo(mainpage): def get(self): self.response.write('welcome, ' + self.request.get('username') + '!') app = webapp2.wsgiapplication([('/', mainpage), ('/broski', thankyo)], debug=true)
Comments
Post a Comment