gpt4 book ai didi

python - Flask:处理表单输入后如何更新div

转载 作者:行者123 更新时间:2023-11-30 22:07:54 25 4
gpt4 key购买 nike

我的 Flask 应用程序根据用户表单输入进行计算。这些计算大约需要 10 秒才能完成。这些计算的输出需要显示在同一页面上表单旁边的 div 中(在图表/表格等中)。

我尝试了两种方法。第一种,仅使用正常的 Flask,重新加载整个页面,这远非理想。第二种方法使用 Sijax,仅更新 div。但在这种情况下,我不知道如何访问表单输入。

我很困惑如何让它发挥作用。希望有任何指示!

方法 1:仅 Flask(缺点:整个页面重新加载)

form_test.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testpage</title>
</head>
<body>
<form action="{{ url_for('do_the_math') }}" method="post">
A = <input type="number" name="input_A">
B = <input type="number" name="input_B">
<input type="submit" value="Submit">
</form>
<div id="destination_div">A + B = {{ result }}</div>
</body>
</html>

app_normal.py:

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route("/")
def show_home():
return render_template("form_test.html", result='unknown yet')

@app.route("/do_the_math", methods=['POST'])
def do_the_math():
A = request.form.get('input_A')
B = request.form.get('input_B')
sum = float(A) + float(B)

# reloads whole page
return render_template("form_test.html", result=sum)

# what i want: reload just update destination_div with new HTML
# return render_template("#destination_div", "A + B = " + str(sum))

if __name__ == '__main__':
app.run(debug=True)

方法 2:使用 Sijax(更新 div,但如何访问表单输入?)

form_test_sijax.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testpage with sijax</title>
<script type="text/javascript" src="/static/js/sijax/sijax.js"></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
</head>
<body>
<form method="post">
A = <input type="number" name="input_A">
B = <input type="number" name="input_B">
<button type="button" onclick="Sijax.request('submit_form');">calc</button>
</form>
<div id="destination_div">A + B = unknown</div>
</body>
</html>

app_sijax.py

from flask import Flask, render_template, g
import flask_sijax
import os

app = Flask(__name__)

# init sijax
app.config["SIJAX_STATIC_PATH"] = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/')
app.config["SIJAX_JSON_URI"] = '/static/js/sijax/json2.js'
flask_sijax.Sijax(app)


def submit_form_handler(obj_response):
A = 5 # how do get to the values entered in the form?
B = 3
sum = A + B
obj_response.html("#destination_div", "A + B = " + str(sum))


@flask_sijax.route(app, "/")
def show_home():
result = 'unknown'
if g.sijax.is_sijax_request:
g.sijax.register_callback('submit_form', submit_form_handler)
return g.sijax.process_request()
return render_template("form_test_sijax.html")


if __name__ == '__main__':
app.run(debug=True)

最佳答案

您可以将 ajaxjquery 结合使用,使用计算结果动态更新页面,而无需刷新页面:

在 html 文件中:

<html>
<header>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</header>
<body>
<div class='wrapper'>
A = <input type="number" id="input_A">
B = <input type="number" id="input_B">
<button class='get_result'>Calculate</button>
<div class='result'></div>
</div>
</body>
<script>
$(document).ready(function(){
$('.wrapper').on('click', '.get_result', function(){
var val1 = $("#input_A").val();
var val2 = $("#input_B").val();
$.ajax({
url: "/calculate_result",
type: "get",
data: {val1: val1, val2:val2},
success: function(response) {
$(".result").html('<p>'+response.result.toString()+'</p>');
},
});
});
});
</script>
</html>

然后,在主应用程序文件中,创建计算最终结果的路由:

@app.route('/calculate_result')
def calculate_result():
a = int(flask.request.args.get('val1'))
b = int(flask.request.args.get('val2'))
return flask.jsonify({"result":a+b})

关于python - Flask:处理表单输入后如何更新div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com