如何为selectfield(wtforms)flask配置html?

时间:2016-12-16 04:43:14

标签: python flask wtforms

每个读这篇文章的人都晚上好。 我想在我的网站上添加一个选择框,但我无法理解如何设置html 我期待看到任何意见和建议:)

我的python代码:

class selectmenu(Form):
    month = SelectField('Choose month',choices=[('dec', 'dec'), ('yan', 'yan'), ('feb', 'febt')])

@app.route('/searchemp/', methods=['GET', 'POST'])
def searchemp():
    form = selectmenu(request.form)
    m = form.month.data

HTML:



<form action="" class="form-signin" method="post">
                    <h2 class="form-signin-heading" align="center">title</h2>
                    <input type="text" class="form-control"
                       placeholder= "username" name="username" value="{{request.form.username}}" required autofocus>
                                <!--
                                <input type="text" class="form-control"
                                               placeholder= "month" name="month" value="{{request.form.month}}">
                                -->
                                                         <select name="month">
                                                         <option value="{{request.form.month}}">dec</option>
                                                         <option value="{{request.form.month}}">yanuary</option>
                                                         <option value="{{request.form.month}}">feb</option>
                                                         <option value="{{request.form.month}}">mar</option>
                                                       </select>
 
                    <button class="btn btn-lg btn-success btn-block" type="submit">Search</button>
                    <br>
                    <p align="center">{{error}} </p>
                </form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

Jinja2模板引擎将选择渲染selectfield,你不必创建一个html选择字段,jinja2已经做了。如果您需要使用validate_on_submit()request.method == 'POST'

来检查表单提交
class SelectMenu(Form):
    month = SelectField('Select Month', choices=[(1, 'January'), (2,'February')])

@app.route('/searchemp/', methods=['GET', 'POST'])
def searchemp():
    form = SelectMenu(request.form)
    if form.validate_on_submit():
        # get posted data
        m = form.month.data
    return render_template('index.html', form=form)

# index.html
<form action="" method="POST">
    {{form.month.label}}{{form.month}}
</form>