gpt4 book ai didi

python - 如何使用 flask 和 wtforms 使单选字段显示默认值

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

我正在使用单选字段,我希望将默认值呈现为 (.) 而不是 ( ) 。我尝试了直接的方法:

choice_switcher = RadioField('Choice?', [validators.Required()], choices=[('choice1', 'Choice One'),('choice2', 'Choice Two')], default='choice1')

它没有用。它将两个选择呈现为:

( ) Choice One
( ) Choice Two

而我想看到这个:

(.) Choice one
( ) Choice Two

最佳答案

对我来说没问题:

例子.py

from flask import Flask, render_template
from flask_wtf import Form
from wtforms import validators, RadioField

app = Flask(__name__)

app.secret_key = 'TEST'

class TestForm(Form):
choice_switcher = RadioField(
'Choice?',
[validators.Required()],
choices=[('choice1', 'Choice One'), ('choice2', 'Choice Two')], default='choice1'
)


@app.route('/')
def hello_world():
testform = TestForm()
return render_template('test_form.html', form=testform)


if __name__ == '__main__':
app.run(debug=True)

test_form.html

{{ form.choice_switcher() }}

生成的 html:

<ul id="choice_switcher">
<li><input checked id="choice_switcher-0" name="choice_switcher" type="radio" value="choice1"> <label
for="choice_switcher-0">Choice One</label></li>
<li><input id="choice_switcher-1" name="choice_switcher" type="radio" value="choice2"> <label
for="choice_switcher-1">Choice Two</label></li>
</ul>

关于python - 如何使用 flask 和 wtforms 使单选字段显示默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25297716/

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