7

我是一名网络开发人员。noobie 和我正在整理一个简单的注册页面,作为在 GoogleApp Engine 上使用 Python 的练习。

当注册表单填写不正确时,程序可以正常工作。正确填写后,它应该重定向到感谢页面,但我收到服务器错误:

Internal Server Error

The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
  File      "/Users/bendavidow/Desktop/Stuff/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEng  ine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1547,   in __call__
return response(environ, start_response)
TypeError: 'unicode' object is not callable

我的问题有两个:(1)这个错误是什么意思?(2) 它来自哪里?

这是代码,减去html:

import webapp2 
import re
import cgi
def escape_html(s):
    return cgi.escape(s, quote = True)

class MainPage(webapp2.RequestHandler):
    def write_form(self, username = "", email = "", username_error = "", password_error   = "", verify_error = "", email_error = ""):
    self.response.out.write(form %{"username": escape_html(username), "email": escape_html(email), "username_error": username_error, "password_error": password_error, "verify_error": verify_error, "email_error": email_error})

def get(self):
    self.write_form()

def post(self):
    username = self.request.get('username')
    password = self.request.get('password')
    verify = self.request.get('verify')
    email = self.request.get('email')


    username_check = re.search(r'^[a-zA-Z0-9_-]{3,20}$', username)
    password_check = re.search(r'^.{3,20}$', password)
    email_check = re.search(r'^[\S]+@[\S]+\.[\S]+$',email)


    username_error, password_error, email_error, verify_error = "", "", "", ""
    if not username_check:
        username_error = "Invalid Username"
    if not password_check:
        password_error = "Invalid Password"
    if email != '' and not email_check:
        email_error = "Invalid Email"
    if password != verify:
        verify_error = "Passwords do not match"

    if username_error or password_error or email_error or verify_error:
        self.write_form(username, email, username_error, password_error, verify_error, email_error)
    else:
        return username
        self.redirect("/thanks")


class ThanksHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("Welcome " + username)

app = webapp2.WSGIApplication([('/', MainPage), ('/thanks',ThanksHandler)], debug = True)
4

1 回答 1

5

Webapp2 期望处理程序方法(get/post)的返回是可调用的。这样做的原因是,您无需更改提供给您的响应对象(self.response,这反过来会修改请求的环境),而是以可调用响应的形式构造一个工作单元,然后由框架应用到请求的环境中。

当您return username框架尝试调用不可调用的 unicode 对象(用户名)时。

于 2012-07-31T17:44:42.843 回答