我正在浏览一个介绍烧瓶的教程,但遇到了一个错误命令行中更完整的错误消息是:“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()
最佳答案
您的代码工作正常,但您误解了/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请求。
关于python - flask -不允许使用方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23543673/