我试图用三个单独的按钮(而不是一个下拉菜单)填充一个烧瓶SelectForm,但是我不确定如何做到。如果有一种方法可以使每个按钮使用不同的颜色,但使用相同的形式,我也将不胜感激。我的代码在下面。
forms.py
class NewForm(FlaskForm):
category = SelectField('Category', choices=[('1', 'Red'), ('2', 'Blue'), ('3', 'Yellow')])
submit = SubmitField('Create')
forms.html
<div class = "form-group">
{{ form.category.label(class = "form-control-label") }}
{% if form.category.errors %}
{{ form.category(class = "form-control form-control-lg is-invalid") }}
<div class = "invalid-feedback">
{% for error in form.category.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.category(class = "btn btn-outline-dark") }}
{% endif %}
</div>
routes.py
@app.route("/form/ methods = ['GET', 'POST'])
def form(client_id):
form = NewForm()
if form.validate_on_submit():
object = Object(category=form.category.data)
db.session.add(object)
db.session.commit()
return redirect(url_for('home'))
return render_template('form.html', form=form)