0
我有一个简单的代码Flask
。我有一个网站有4个按钮,当按下后发送POST到Flask
并返回相同的页面,但用另一种颜色收紧的按钮。每个按钮的状态都存储在布尔数组中。
这是Flask
代码:重新加载页面重新发送数据
import numpy as np
from flask import Flask, request, render_template
app = Flask(__name__)
states = np.array([0, 0, 0, 0], dtype=bool)
@app.route('/control', methods=['GET', 'POST'])
def control():
if request.method == 'POST':
val = int(request.form['change rele state'])
states[val] = not states[val]
return render_template('zapa.html', states=states)
else:
return render_template('zapa.html', states=states)
if __name__ == '__main__':
app.run(debug=True)
和页面:
{% extends "layout.html" %}
{% block content %}
<h2>Control</h2>
<p>Botones</p>
<p>{{ states }}</p>
<form action="/control" method="POST">
{% for state in states %}
{% if state == True %}
<button class="btn btn-primary" type="submit" name="change rele state" value="{{ loop.index0 }}">Enchufe {{ loop.index }} Off</button>
{% endif %}
{% if state == False %}
<button class="btn btn-danger" type="submit" name="change rele state" value="{{ loop.index0 }}">Enchufe {{ loop.index }} On</button>
{% endif %}
{% endfor %}
</form>
{% endblock %}
的问题是,按重新加载页面,仿佛按下按钮时发送。为什么?我如何避免这种情况?