我正试图通过GAE运行一个简单的GuestBook页面。它显示一个文本字段和旁边的一个按钮。当按下按钮时,POST方法似乎有错误。这个代码对我来说很好,我知道这只是一个小错误,我只是似乎找不到它。
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write("""<h1>Hello, world.</h1>
<br> <form action="/sign" method=post>
<textarea name=content rows=2 cols=30></textarea>
<br><input type=submit value="Sign GuestBook">
</form>""")
class GuestBook(webapp2.RequestHandler):
def post(self):
self.response.write('<h2>You wrote:</h2> %s' % self.request.get('content'))
APP = webapp2.WSGIApplication([
('/.*', MainHandler),
('/sign', GuestBook),
], debug=True)
来自URI路由:
它就是这样工作的:在WSGI中注册一个路由列表应用当应用程序收到请求时,它会尝试按顺序匹配每个,直到其中一个匹配,然后调用相应的处理程序。
在您的情况下,MainHandler
的'/.*'
模式也与GuestBook
的'/sign'
模式相匹配,并且在APP
的列表中被放置在之前,因此调用MainHandler
而不是您所期望的GuestBook
。并且MainHandler
没有post()
方法,因此出现了错误。
要修复它,只需交换APP
列表中的模式顺序:
APP = webapp2.WSGIApplication([
('/sign', GuestBook),
('/.*', MainHandler),
], debug=True)