TAGS :Viewed: 6 - Published at: a few seconds ago

[ Flask - Method not allowed ]

I'm running through an intro Flask tutorial and hitting an error. ' The fuller error message in the command line is: "GET /signup HTTP/1.1" 405". From these two files any idea where I am going wrong? http://opentechschool.github.io/python-flask/core/form-submission.html

<!-- index.html in the templates folder -->
<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>Cats Everywhere!</title> 

&lt;link href='http://fonts.googleapis.com/css?family=Sintony:400,700' rel='stylesheet' type='text/css'&gt;

&lt;style type="text/css"&gt;    

body { background-color:#000; }

h1 { font-size:48px; margin-top:0; font-family:Arial, sans-serif; text-shadow:2px 0 15px #292929; letter-spacing:4px; text-decoration:none; color:#DDD; }

#banner { width:500px; height:200px; text-align:center; background-image:url(http://i.imgur.com/MQHYB.jpg); background-repeat:no-repeat; border-radius:5px; margin:90px auto auto; padding:80px 0; }

.lead { background-color:rgba(255,255,255,0.6); border-radius:3px; box-shadow:rgba(0,0,0,0.2) 0 1px 3px; font-family:Sintony, sans-serif; } </style>


<body> <div id="banner"> <h1>cats everywhere</h1> <p class="lead">We're bringing cats to the internet. Free. Cute. Awesome.</p> </div> <div id="emailform"> <form action="/signup" method="post"> <input type="text" name="email"></input> <input type="submit" value="Signup"></input> </form> </div> </body> </html>


catseverywhere.py file:

from flask import Flask, render_template from flask import request, redirect

app = Flask(name)

@app.route('/') def helloworld(): author = "Me" name = "RandomName" return rendertemplate('index.html', author=author, name=name)

@app.route('/signup', methods = ['POST']) def signup():
email = request.form['email'] print("The email address is '" + email + "'") return redirect('/')

if name == 'main': app.run()


Answer 1

Your code works fine, but you misunderstood how the /signup route works.

The homepage holds the form; visit http://localhost:5000/ in your browser and you'll see a white textbox and signup button there. Text you fill into that box will be posted to the server

The console would look like:

 * Running on - - [08/May/2014 15:00:53] "GET / HTTP/1.1" 200 - - - [08/May/2014 15:00:53] "GET /favicon.ico HTTP/1.1" 404 -
The email address is 'abcd' - - [08/May/2014 15:00:57] "POST /signup HTTP/1.1" 302 - - - [08/May/2014 15:00:57] "GET / HTTP/1.1" 200 -

where GET / is the browser fetching the homepage with form, the POST /signup is the form being posted, which then issues a 302 redirect back to the homepage, which is fetched by the browser next.

The /signup route, as configured, can only handle a POST request, like what the browser produces for the form result. You don't normally visit it in the browser; entering http://localhost:5000/signup produces a GET request instead.

In other words, the error you see is by design; the route only supports POST requests.