烧瓶 - 方法不允许

时间:2014-05-08 13:48:27

标签: python flask

我正在浏览介绍Flask教程并遇到错误。 '命令行中更完整的错误消息是:" GET / signup HTTP / 1.1" 405&#34 ;.从这两个文件中我知道哪里出错了? http://opentechschool.github.io/python-flask/core/form-submission.html

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

    <link href='http://fonts.googleapis.com/css?family=Sintony:400,700' rel='stylesheet' type='text/css'>

    <style type="text/css">    
  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>

</head>  

<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 hello_world():
    author = "Me"
    name = "RandomName"
    return render_template('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()

1 个答案:

答案 0 :(得分:3)

您的代码运行正常,但您误解了/signup路径的工作方式

主页持有表格;在浏览器中访问http://localhost:5000/,您会看到一个白色文本框和signup按钮。填写到该框中的文本将发布到服务器

控制台看起来像:

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

其中GET /是浏览器使用表单获取主页时,POST /signup是发布的表单,然后向主页发出302重定向,然后由浏览器提取。< / p>

配置的/signup路由只能处理POST请求,就像浏览器为表单结果生成的那样。您通常不会在浏览器中访问它;输入http://localhost:5000/signup会产生GET请求。

换句话说,您看到的错误是按设计;该路由仅支持POST请求。