我是python和Flask的新手,我试图在我的计算机上运行此页面中显示的代码: http://runnable.com/UhLMQLffO1YSAADK/handle-a-post-request-in-flask-for-python
这是我遵循的陡峭和代码:
1 - 我已经安装了Flask
2-文件
文件app.py
# We need to import request to access the details of the POST request
# and render_template, to render our templates (form and response)
# we'll use url_for to get some URLs for the app on the templates
from flask import Flask, render_template, request, url_for
# Initialize the Flask application
app = Flask(__name__)
# Define a route for the default URL, which loads the form
@app.route('/')
def form():
return render_template('form_submit.html')
# Define a route for the action of the form, for example '/hello/'
# We are also defining which type of requests this route is
# accepting: POST requests in this case
@app.route('/hello/', methods=['POST'])
def hello():
name=request.form['yourname']
email=request.form['youremail']
return render_template('form_action.html', name=name, email=email)
# Run the app :)
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("80")
)
文件form_action.html
<html>
<head>
<title>Handle POST requests with Flask</title>
<link rel=stylesheet type=text/css href="style.css">
</head>
<body>
<div id="container">
<div class="title">
<h1>POST request with Flask</h1>
</div>
<div id="content">
Hello <strong>{{name}}</strong> ({{email}})!
</div>
</div>
</div>
</body>
</html>
文件form_submit.html
<html>
<head>
<title>Handle POST requests with Flask</title>
<link rel=stylesheet type=text/css href="style.css">
</head>
<body>
<div id="container">
<div class="title">
<h1>POST request with Flask</h1>
</div>
<div id="content">
<form method="post" action="{{ url_for('hello') }}">
<label for="yourname">Please enter your name:</label>
<input type="text" name="yourname" /><br />
<label for="youremail">Please enter your email:</label>
<input type="text" name="youremail" /><br />
<input type="submit" value="Send" />
</form>
</div>
</div>
</div>
</body>
</html>
3 - 我运行py文件:
sudo python app.py
[sudo] password for jose:
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
当我打开浏览器时,我写道: 文件:///home/jose/Escritorio/python/app/form_submit.html
我在2个表格中插入数据,然后按发送,这就是:
网址:file:/// home / jose / Escritorio / python / app / {{url_for(&#39; hello&#39;)}}
网页:找不到文件
我做错了什么?
答案 0 :(得分:0)
0.0.0.0表示您可以从网站主机外部访问烧瓶网站。使用主机IP加上您指定的端口# http://:80 /你好。这应该显示您在路线中指定的form_action.html。
答案 1 :(得分:-1)
如果您想保存表单数据,则代码无效。您必须拥有数据库或保存在文件中。