尝试使用flask-bootstrap quick_form。
HTML模板my-form.j2:
{% import "bootstrap/wtf.html" as wtf %}
{% extends "layout.j2" %}
{% block content %}
<h1>My form</h1>
{{ wtf.quick_form(form) }}
{% endblock content %}
表格类:
class MyForm(FlaskForm):
"""A WTForm for configuring vehicle information"""
field1 = StringField('First field', [wtforms.validators.required()])
field2 = StringField('Second field', [wtforms.validators.required()])
field3 = StringField('Third field', [wtforms.validators.required()])
表单处理程序:
@app.route('/myform', methods=('GET', 'POST'))
def my_form():
form = MyForm()
if form.validate_on_submit():
myData = form.data
return redirect("/")
return render_template("my-form.j2", form=form)
不幸的是,我没有看到呈现提交按钮,即使我添加了
{{ form.submit }}
到我的桌子。
有什么想法吗?
答案 0 :(得分:3)
@atwalsh的答案工作得很好,但你也可以把你的提交按钮放在你的课堂上,然后在你的模板中调用它:
class MyForm(FlaskForm):
field1 = StringField('First field', [wtforms.validators.required()])
field2 = StringField('Second field', [wtforms.validators.required()])
field3 = StringField('Third field', [wtforms.validators.required()])
--> submit_button = SubmitField('Submit Form')
然后输入你的模板:
{{ wtf.quick_form(form, button_map={'submit_button': 'primary'}) }}
^ ^
答案 1 :(得分:1)
我相信您可以在{{ wtf.quick_form(form) }}
之后直接添加输入字段,并将这两个字段放在HTML表单中:
<input class="btn btn-primary" type="submit" value="Login">
示例强>
{% import "bootstrap/wtf.html" as wtf %}
{% extends "layout.j2" %}
{% block content %}
<h1>My form</h1>
<form method="POST" action="/">
{{ wtf.quick_form(form) }}
<input class="btn btn-primary" type="submit" value="Login">
</form>
{% endblock content %}