我对Bootstrap和Flask还是很陌生,我试图提供三个按钮选项,它们用已经被硬编码的特定数据填充了Flask表单(例如:黄色按钮== 1,红色按钮== 2,蓝色按钮== 3,依此类推),但我不确定如何操作。我在下面发布了一些代码,如果您需要更多代码,请这样做。
表格
class NewClientForm(FlaskForm):
name= StringField('Name', validators=[DataRequired()])
category = StringField('Category') <-- THE ONE I WANT TO CHANGE TO BUTTONS
notes = TextAreaField('Notes')
submit = SubmitField('Create')
form.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 = "form-control form-control-lg") }}
{% endif %}
</div>
route.py
@app.route("/new_client", methods = ['GET', 'POST'])
def new_client():
form = NewClientForm()
if form.validate_on_submit():
client = Client(name=form.name.data, category=form.category.data, notes=form.notes.data)
db.session.add(client)
db.session.commit()
return redirect(url_for('home'))
现在我只能提交书面数据,而不能提交硬编码数据。
This is how it looks
先谢谢了
最佳答案
从https://wtforms.readthedocs.io/en/stable/fields.html#wtforms.fields.SubmitField看来,您必须创建每个具有不同名称的SubmitField
,并在服务器上检查字段的True值,作为评论的答案。
我认为该框架不支持ButtonField
之类的内容,因此不存在上述解决方案。
关于python - 如何将特定 flask 形式的数据链接到 Bootstrap 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59004232/