gpt4 book ai didi

python - 如何为 selectfield(wtforms) Flask 配置 html?

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:17 25 4
gpt4 key购买 nike

各位阅读这篇文章的人晚上好。我想在 Flask 中向我的网站添加一个选择框,但我不明白如何为此设置 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>

最佳答案

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>

关于python - 如何为 selectfield(wtforms) Flask 配置 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41177396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com