WTForms SelectField中的自定义选项在当前日期进行

时间:2015-06-12 03:37:03

标签: python flask wtforms

我想创建一个自定义 $test= array('bob' => '','mike' => '','tara' => 'serverName'); while(list($key,$value) = each($test)) { if($value==$_SERVER['SERVER_NAME']) { echo $key; break; } } ,根据当前日期提供不同的选择。例如,如果它是该月的第13个,则选项将是值1到13.我该怎么做?

SelectField

1 个答案:

答案 0 :(得分:4)

覆盖表单的__init__方法,并使用从1到当天的值范围填充字段choices

from datetime import datetime

class ExtendedRegisterForm(RegisterForm):
    pay_month = SelectField()

    def __init__(self, *args, **kwargs):
        super(ExtendedRegsiterForm, self).__init__(*args, **kwargs)
        now = datetime.utcnow()
        self.pay_month.choices = [(i, i) for i in range(1, now.day + 1)]