Python webapp2 中的“405 方法不允许”

"405 Method Not Allowed" in Python webapp2

import webapp2

form="""
     <form method="post">
          <input type="" name="day">
          <input type="" name="month">
          <input type="" name="year">
          <input type="submit" name="">
     </form>
     """
class MainPage(webapp2.RequestHandler):

def get(self):
    self.response.out.write(form)

def post(self):
    self.response.out.write("thank")

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

此代码响应

405 Method Not Allowed

The method POST is not allowed for this resource.

这与您的配置有关。我测试了您的代码几乎与您发布的代码相同并且它响应正确(使用 google appengine)。

class MainPage(webapp2.RequestHandler):
    form = """
         <form method="post">
              <input type="" name="day">
              <input type="" name="month">
              <input type="" name="year">
              <input type="submit" name="">
         </form>
         """
    def get(self):
        self.response.out.write(self.form)

    def post(self):
        self.response.out.write("thank")


app = microwsgi.MicroWSGIApplication([
                                      ('/MainPage', MainPage)...

我遇到了同样的问题。您使用的编辑器对 "indentation" 的配置有误,这对 python 的解释器正确解释代码至关重要。

尝试用 Python IDE 重写这个程序。