【发布时间】:2020-04-23 09:48:14
【问题描述】:
在下面的示例中:频率无线电默认设置为 1。
当我到达 /preference 时,我会将值修改为适当的频率
然后将信息保存在适当的变量中。
到这里为止,一切都很好。
现在,我怎样才能将此信息填充到表单中?
为了下次我到达 /preference 时,频率单选按钮将设置为最后一个选项。
谢谢
# -------------------
# File : forms.py
# -------------------
from flask_wtf import FlaskForm
from wtforms import StringField, RadioField , SubmitField
from webapp import app
class preferenceForm(FlaskForm):
frequence = RadioField(
'Frequence',choices=[
(1,'4800 Hz'),
(2,'8000 Hz'),
(3,'11025 Hz'),
(4,'44100 Hz')
],
default=1,
coerce=int)
submit = SubmitField('Save')
# -------------------
# File : preference.html
# -------------------
{% extends "base.html" %}
{% block content %}
<h1>Preferences for tap2wav</h1>
<form action="" method="post">
{{ form.hidden_tag() }}
<p>
{{ form.frequence.label }}<br>
{{ form.frequence}}
</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
# -------------------
# File : routes.py
# -------------------
@app.route('/preferences', methods=['GET', 'POST'])
def preference():
form = preferenceForm()
if form.validate_on_submit():
app.config["TAP2WAV_FORM_FREQUENCE_ID"] = form.frequence.data
########################################################################################
# QUESTION
# HOW TO POPULATE THE INFORMATION TO THE RADIO BUTTON FORM IN ORDER THEN NEXT /preference
# THE DEFAULT VALUE WILL BE SET TO : form.frequence.data
########################################################################################
return redirect('/')
# Pass the template data into the template main.html and return it to the user
return render_template("preference.html", AppConfig=app.config, form=form)
【问题讨论】:
标签: python html flask-wtforms wtforms