Get selected text from a form using wtforms SelectField

后端 未结 5 1109
别跟我提以往
别跟我提以往 2020-12-17 21:02

This is a question upon the use of wtforms SelectField.

Once the form submitted, I wish to extract selected text.

I have the following form:

5条回答
  •  别那么骄傲
    2020-12-17 21:38

    Define choices global in forms:

    HOUR_CHOICES = [('1', '8am'), ('2', '10am')]
    
    class TestForm(Form):
         hour = SelectField(u'Hour', choices=HOUR_CHOICES)
    

    import it from forms, convert it to dict:

    from .forms import HOUR_CHOICES
    
    hour_display = dict(HOUR_CHOICES).get(form.hour.data)
    

提交回复
热议问题