【问题标题】:Flask - Method not allowed烧瓶 - 不允许的方法
【发布时间】:2014-05-08 22:05:34
【问题描述】:

我正在浏览 Flask 入门教程并遇到错误。 ' 命令行中更完整的错误消息是:“GET /signup HTTP/1.1”405”。从这两个文件中知道我哪里出错了吗? 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()

【问题讨论】:

  • 返回 405 的确切 URL 是什么? Flask 在控制台中告诉你访问了哪些 URL。
  • 命令行中更完整的错误信息是:"GET /signup HTTP/1.1" 405
  • 不过,您可能希望将其添加到您的帖子中。
  • 我无法使用您发布的代码重现您的错误;表格按设计工作。控制台打印The email address is 'abcd'127.0.0.1 - - [08/May/2014 15:00:57] "POST /signup HTTP/1.1" 302 -(302 是重定向)。
  • 很高兴知道。谢谢。

标签: python flask


【解决方案1】:

您的代码运行良好,但您误解了/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 重定向回到主页,然后由浏览器获取。

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

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

【讨论】:

  • 非常感谢。而已。我从根本上误解了事情的运作方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 2014-03-08
  • 2014-07-28
  • 2020-07-30
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多