What is producing “405 Method Not Allowed” in this

2019-05-11 11:11发布

I'm a python newbie and I'm trying to build an app copying step by step what was taught earlier in my class, but I'm getting the "405 Method Not Allowed" error.

Here what the professor did: enter image description here

Here what I did:

enter image description here

Could someone point me what in the code below is the cause of this error "405 Method Not Allowed"? I can not see difference between what I did and what the professor taught. The indentation is also ok (here is the main.py file https://docs.google.com/open?id=0B8TXLR_e14aCVDFfdlpYSU9DNDg).

Thanks in advance for any help!

Here my code:

form= """
  <html>
  <head>
    <title>Unit 2 Rot 13</title>
  </head>

  <body>
    <h2>Enter some text to ROT13:</h2>
    <form method="post" action="/rot13">
      <textarea name="text"
                style="height: 100px; width: 400px;"></textarea>
      <br>
      <input type="submit">
    </form>
  </body>

  </html> """

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)

class Rot13Handler(webapp2.RequestHandler):
    def post(self):
        text = self.request.get("text")
        self.response.out.write(text)

app = webapp2.WSGIApplication([('/', MainHandler), ('/rot13', Rot13Handler)],
                          debug=True)

6条回答
乱世女痞
2楼-- · 2019-05-11 11:27

I had the same problem with my code which was fixed when I rearranged the order of the handlers. Went through several different SO answers checking for errors in Handler definition, indentation and finally fixed it on looking at the order of the handlers.

查看更多
再贱就再见
3楼-- · 2019-05-11 11:29

I am just trying Python following Udacity's online course and met similar issue that the AppEngine unable to find the post method.

And finally it turns out that the root cause is the INDENTION.

I used Notepad++ as the editor for the small project and it just can't work, keep throwing the 405 error. then I copy and paste the code to Netbean IDE with Python plugin installed, the IDE showed that it was wrong indention that made the POST method an inner method of GET method, which could not be found in Notepad++, although it looked like the indention been handled well.

查看更多
疯言疯语
4楼-- · 2019-05-11 11:32

had the same problem. The issue was indentation again. When defining:

def post(self):

if I used 'tab' for indentation it was not working. When I used spaces it did. The error logs showed nothing. To avoid issues like this, you can use a python IDE, like Wing IDE.

查看更多
孤傲高冷的网名
5楼-- · 2019-05-11 11:46

I had the same issue with Notepad++. Only thing that I changed with the Python IDLE was to replace some spaces with a tab and it worked fine :)

查看更多
做自己的国王
6楼-- · 2019-05-11 11:47

The code is correct and works fine. You need to look elsewhere for an explanation of this 405 error.

EDIT

Have you posted your actual code? This code below will give you a 405 Method not Allowed error when you click submit. It has a subtle error in it... :)

import webapp2

form= """
  <html>
  <head>
    <title>Unit 2 Rot 13</title>
  </head>

  <body>
    <h2>Enter some text to ROT13:</h2>
    <form method="post" action="/rot13">
      <textarea name="text"
                style="height: 100px; width: 400px;"></textarea>
      <br>
      <input type="submit">
    </form>
  </body>

  </html> """

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(form)

class Rot13Handler(webapp2.RequestHandler):
    # Error here: mistyped get instead of post :)
    def get(self):
        text = self.request.get("text")
        self.response.out.write(text)


app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/rot13', Rot13Handler)],
                              debug=True)

And the same would happen if your routing is incorrectly typed, as in:

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

EDIT (Thanks, @Nick Johnson)

If none of the above works, consider starting from scratch and check your GAE set-up.

  1. Do you have a valid app.yaml file alongside the main.py module?
  2. Are you able to run the guestbook demo app in the standard Google AppEngine installation?
  3. If not, post the error messages, if any, as well as the details of the system that you are running it on.
  4. If you are able to run the guestbook, can you try and rebuild your application by editing that one? I have found that this has worked for me in the past.
查看更多
Summer. ? 凉城
7楼-- · 2019-05-11 11:47

Ran into the same problem using Notepad++ . Took me days to figure out what was wrong till I read this. Switched to using Python IDLE. Works like a charm. I'll echo @Aris and say that using a 'traditional' Python IDE is the best way to go.

查看更多
登录 后发表回答