我的代码是这样的:
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 and password and verify and 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 a valid username."
self.passworderror = ''
if not re.compile(r"^.{3,20}$").match(self.request.get('password')):
self.passworderror = "That wasn't a valid password."
self.verifyerror = ''
if self.request.get('verify') != self.request.get('password') and 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 a 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)
这实际上是我第一次为超过 10 行的 GAE(而且我是 python 新手..)编写代码,当然,他打破了。我发现了几个错误,但绝对可以帮助,因为该网站甚至还没有加载。非常感谢有关代码的帮助。更新:我更新了一点(更改了ThankYo的继承),现在表单成功加载,一旦我发布数据,它就会崩溃......