【问题标题】:How to configure html for selectfield(wtforms) flask?如何为 selectfield(wtforms) 烧瓶配置 html?
【发布时间】:2016-12-16 04:43:13
【问题描述】:

祝所有阅读这篇文章的人晚上好。 我想在烧瓶中向我的网站添加一个选择框,但我不明白如何为此设置 html 我期待看到任何 cmets 和建议:)

我的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>

【问题讨论】:

  • 第一行缺少&lt;form
  • 我已经编辑了这个问题。你能提出任何建议吗?

标签: python flask wtforms


【解决方案1】:

Jinja2 模板引擎将呈现带有选项的选择字段,您不必创建一个 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2012-08-19
    • 2013-03-16
    • 2016-05-03
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    相关资源
    最近更新 更多