我是网络开发者。 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)
答案 0 :(得分:5)
Webapp2期望从处理程序方法(获取/发布)to be callable返回。这样做的原因是,不是改变提供给你的响应对象(self.response,它反过来修改了请求的环境),而是以可调用响应的形式构造一个工作单元,然后框架applied onto the environment of the request。
当return username
框架尝试调用您的不可调用的unicode对象(用户名)时。