【发布时间】:2019-07-08 13:01:18
【问题描述】:
我正在使用 python flask 将表单数据发送到另一个 HTML 模板。我需要在存在 HTML 输入表单的同一页面中显示结果。目前正在将响应转发到下一页。
我尝试通过 JQuery 运行但仍然无法得到结果。
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def student():
return render_template('index.html')
@app.route('/result',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
return render_template("result.html",result = result)
if __name__ == '__main__':
app.run(debug = True)
index.html
<html>
<body>
<form action = "http://localhost:5000/result" method = "POST">
<p>Name <input type = "text" name = "Name" /></p>
<p>Physics <input type = "text" name = "Physics" /></p>
<p>Chemistry <input type = "text" name = "chemistry" /></p>
<p>Maths <input type ="text" name = "Mathematics" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
<div>
</div>
</body>
</html>
结果.html
<!doctype html>
<html>
<body>
<table border = 1>
{% for key, value in result.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
</body>
</html>
现在结果显示在 result.html 中,我需要将其显示在 index.html 中。请提出建议,如何实现这一点。
【问题讨论】: