简体   繁体   English

如何为selectfield(wtforms)烧瓶配置HTML?

[英]How to configure html for selectfield(wtforms) flask?

Good evening to everyone who reads this post. 大家晚上好,阅读这篇文章。 I want to add a select box to my web site in flask, but i can not understand how to set up html for that I look forward to see any comments and suggestions :) 我想在我的烧瓶网站上添加一个选择框,但我不明白如何设置HTML,我希望看到任何评论和建议:)

My python code: 我的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: 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> 

Jinja2 template engine will render selectfield with choices, you dont have to create a html select field, jinja2 already does. Jinja2模板引擎将呈现带有选择的selectfield,您不必创建html select字段,jinja2已经做到了。 And if you need to check form submission use validate_on_submit() or request.method == 'POST' : 如果您需要检查表单提交,请使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM