Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - POST Error 405 Method Not Allowed

I'm just starting to learn Flask, and I am trying to create a form which will allow a POST method.

Here's my method:

@app.route('/template', methods=['GET', 'POST']) def template():     if request.method == 'POST':         return("Hello")     return render_template('index.html') 

And my index.html:

<html>  <head>   <title> Title </title> </head>  <body>   Enter Python to execute:   <form action="/" method="post">     <input type="text" name="expression" />     <input type="submit" value="Execute" />   </form> </body>  </html> 

Loading the form (rendering it when it receives GET) works fine. When I click on the submit button however, I get a POST 405 error Method Not Allowed.

Why isn't it displaying "Hello"?

like image 353
darksky Avatar asked Aug 29 '12 13:08

darksky


People also ask

Why am I getting 405 Method not allowed?

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method. The server must generate an Allow header field in a 405 status code response.

Why POST method is not allowed?

The Request Method' POST' Not Supported error is caused by a mismatch of the web browser configuration and the browser's URL format. In this case, the browser sends a URL request, the web server receives and recognizes the URL but cannot execute commands or grant access to the requested page.


1 Answers

Your form is submitting to / when the method is routed for /template unless that is a typo, you should adjust your form's action attribute to point at the template view: action="{{ url_for('template') }}"

like image 108
Burhan Khalid Avatar answered Oct 01 '22 03:10

Burhan Khalid