Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python WTForms无线字段默认值_Python_Wtforms - Fatal编程技术网

Python WTForms无线字段默认值

Python WTForms无线字段默认值,python,wtforms,Python,Wtforms,我正在使用wtforms生成一个html表单,如下所示: <div class="control-group"> {% for subfield in form.time_offset %} <label class="radio"> {{ subfield }} {{ subfield.label }} </label> {% endfor %} </div> class SN46

我正在使用wtforms生成一个html表单,如下所示:

<div class="control-group">
    {% for subfield in form.time_offset %}
    <label class="radio">
        {{ subfield }}
        {{ subfield.label }}
    </label>
    {% endfor %}
</div>
class SN4639(Form):
    time_offset = RadioField(u'Label', choices=[
        ('2', u'Check when Daylight saving has begun, UTC+02:00'),
        ('1', u'Check when Daylight saving has stopped, UTC+01:00')],
        default=2, validators=[Required()])

当我现在打开编辑表单时,我通过SQL获得值1或2-我如何预设特定单选按钮?

如果我正确理解您的问题,您希望使用预先选择的选项呈现表单(而不是在没有向表单提交值的情况下返回默认选项)

您可以在设置预选值的同时构造表单:

myform = SN4639(time_offset='2')

然后将
myform
传递给要呈现的模板。

Form.\uuuu init\uuuu
接受关键字参数
obj=
,如果没有提供formdata或其他默认值,该参数将从给定对象填充表单。将结果从数据库传递到该数据库,它应该可以工作。

default=2需要为string类型,而不是int类型:

class SN4639(Form):
    time_offset = RadioField(u'Label', choices=[
        ('2', u'Check when Daylight saving has begun, UTC+02:00'),
        ('1', u'Check when Daylight saving has stopped, UTC+01:00')],
        default='2', validators=[Required()])
我的朋友

实例化表单后,需要用数据库中的数据填充.data字段。看看这个例子:

my_instance = SN4639()
my_db_id = get_id_on_my_db()  # fill the variable with the database id

my_instance.time_offset.data = my_db_id  # here's the cat jump
time_offset = RadioField(u'Label', choices=[
    (2, u'Check when Daylight saving has begun, UTC+02:00'),
    (1, u'Check when Daylight saving has stopped, UTC+01:00')],
    coerce=int,
    default=2, validators=[Required()])
现在,当您在HTML中使用my_instance.time_offset()时,将选择正确的收音机

重要注意事项:

如果id是整数,则必须在RadioField中添加参数强制=int。看看这个例子:

my_instance = SN4639()
my_db_id = get_id_on_my_db()  # fill the variable with the database id

my_instance.time_offset.data = my_db_id  # here's the cat jump
time_offset = RadioField(u'Label', choices=[
    (2, u'Check when Daylight saving has begun, UTC+02:00'),
    (1, u'Check when Daylight saving has stopped, UTC+01:00')],
    coerce=int,
    default=2, validators=[Required()])

如果您想在表单上设置默认值,您想在html中设置默认值的原因是什么?你想要两个默认值吗?在创建表单中,我在类中设置了默认值,如你所见。但是现在我正在创建一个编辑表单,它应该被预先选中。e、 g.“男性/女性”,当我从数据库中获得值“m”时,应选择男性。。。我该怎么做?default=yourObjectFromDB.sex???嗨,对不起,也许我要新建或转储。。。我不明白。您是在类中还是在模板中设置的?不过,看起来我这个周末会继续尝试。lel,我知道你像往常一样设置了课堂,但attache uu最后那是因为“课堂”是一个保留词。因此,您可以在模板中设置类,如class=“someclass”