我对python和Flask是完全陌生的,我正在尝试在我的计算机中运行本页中显示的代码: http://runnable.com/UhLMQLffO1YSAADK/handle-a-post-request-in-flask-for-python
这是我所遵循的陡坡和代码:
1-我已经安装了烧瓶
2个文件
文件应用程序副本
# 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")
)
文件格式_动作.html
^{pr2}$文件格式_提交.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-I运行py文件:
sudo python app.py
[sudo] password for jose:
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
当我打开浏览器时,我会写下: file:///home/jose/Escritorio/python/app/form_提交.html在
我在两个表单中插入数据,然后按Send,结果如下:
URL:file:///home/jose/Escritorio/python/app/{URL_for('hello')}}
网页:找不到文件
我做错什么了?在
0.0.0.0意味着您可以从网站主机外部访问flask网站。使用主机ip加上您指定的端口 http://:80/您好。应该显示表单_动作.html你在你的路线上指定了。在
如果你想保存表单数据,你的代码不起作用。必须有数据库或保存在文件中。在
相关问题 更多 >
编程相关推荐