0

我的代码是这样的:

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的继承),现在表单成功加载,一旦我发布数据,它就会崩溃......

4

1 回答 1

1
class ThankYo(Mainpage):

应该

class ThankYo(MainPage):

(注意大写P)。


我通过查看终端中留下的错误消息发现了这一点:

% dev_appser  File "/home/unutbu/pysrc/project/main.py", line 38, in <module>
...
    class ThankYo(Mainpage):   
NameError: name 'Mainpage' is not defined

也改变

self.reply(usererror,passworderror,verifyerror,emailerror)

self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror)

我通过查看终端错误消息再次发现了这一点:

  File "/home/unutbu/pysrc/project/main.py", line 39, in post
    self.reply(usererror, passworderror, verifyerror, emailerror)
NameError: global name 'usererror' is not defined

如何查看错误消息(来自终端):

启动 GAE 服务器的方法不止一种。我这样做的方法是打开终端并运行

dev_appserver.py /path/to/project/

然后终端会发出有用的错误消息。


最后一个问题有点复杂。我们正在向该reply方法发送错误消息:

self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror)

但在reply方法定义中,我们称它们为user、和。这会导致一些认知失调。让我们重命名它们,和:passwordverifyemailusererrorpassworderrorverifyerroremailerror

def reply(self, usererror, passworderror, verifyerror, emailerror):

现在检查没有错误使用:

if not any(usererror and passworderror and verifyerror and emailerror):

但是我们要重定向到的页面需要用户名!如果我们只是重定向到/broksi

        self.redirect('/broski')

那么ThankYo处理程序不知道用户名是什么。self.request.get('username')会出现空白(返回一个空字符串)。所以我们需要将用户名传递给broski

import urllib

self.redirect('/broski?' + urllib.urlencode(
            {'username' : self.username}))

所以代码最终看起来像这样:

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 and passworderror and verifyerror and 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 a valid username."
        self.passworderror = ''
        if not re.compile(r"^.{3,20}$").match(self.password):
            self.passworderror = "That wasn't a valid password."
        self.verifyerror = ''
        if self.verify != self.password and 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 a 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)
于 2013-04-01T12:36:25.757 回答