Вы можете использовать Post / Redirect / Get .
Используйте форму в index.html
, чтобы отправить значение в @app.route('/index/', methods=['GET', 'POST'])
в app.py
Форма в index.html
variable = {{ variable }}
<form action="{{ url_for('index') }}" method="post">
<input type="text" value="101" name="variable">
<input type="submit" value="set variable to 101">
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li style="color:red">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
изменить app.py
для обработкиформа
from flask import Flask, render_template, request, redirect, url_for, flash
app = Flask(__name__)
app.secret_key = b'_1#y2l"F4Q8z\n\xec]/'
variable = 100
@app.route('/', methods=['GET', 'POST'])
@app.route('/index/', methods=['GET', 'POST'])
def index():
global variable
if request.method == "POST":
try:
variable = int(request.form.get("variable"))
return redirect(url_for('index'))
except:
flash("Invalid type for variable")
return redirect(url_for('index'))
return render_template('index.html', variable=variable)
if __name__ == '__main__':
app.run(debug=True, port=5000)
Добавление app.secret_key = b'_1#y2l"F4Q8z\n\xec]/'
к flash
сообщению об ошибке, если пользователь пытается отправить что-либо кроме цифр