0
我正在尝试编写代码,它将为页面请求数据,处理它,然后在不刷新的情况下在页面的较低部分显示结果。在Flask的同一页面上显示结果
瓶代码:
if request.method == 'GET':
return render_template("dashboard.html", data_nodes=data_nodes, data_para=data_para)
elif request.method=='POST':
#c, conn = connection2()
select1 = request.form.get('node')
select2 = request.form.get('parameter')
print str(select1)
print str(select2)
#processes the data
#display the processed data on the same page
#return render_template("dashboard.html") This will refresh the page! Is there another way?
我的HTML侧
<form method="POST" action="{{ url_for('dashboard')}}">
<label for="node">Node</label>
<select name="node">
{% for o in data_nodes %}
<option value="{{ o.name }}">{{ o.name }}</option>
{% endfor %}
</select>
<label for="parameter">Parameter</label>
<select name="parameter">
{% for o in data_para %}
<option value="{{ o.name }}">{{ o.name }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-default" >Send</button>
</form>
有没有一种方法,而无需重定向到另一个页面后处理的数据?
请记住,当您想要在页面中显示某些内容而无需刷新时,您需要在页面上使用Javascript进行所有更新。所以要做你想做的事情的方法是制作一个JavaScript来调用你的服务,然后用收到的数据来更新你的部分。 – Aquiles
这是否意味着JavaScript将首先获取数据,并将其传递给Flask,然后Flask将处理数据并将其发送回JavaScript?那可能吗? –
是的,这样做的方法是让Javascript,AJAX发送表单到你的瓶子后期服务,然后处理你的服务的响应。查看答案中的示例@sashadereh – Aquiles