Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value (not key) data from SelectField in WTForms [duplicate]

I have a WTF SelectField, and I'm trying to store the name of the user's choice for display on another page.

Given that my form is

choice = SelectField('Choice', choices=[('cho_1', 'Choice One'), ('cho2', 'Choice Two')])

I understand that

self.choice = form.choice.data

will get me the user's choice (say, cho_1), but how do I get the value ("Choice One")? I feel like it's something simple with dicts, but various attempts plus googling/searching SO haven't helped so far.

like image 833
Josh Friedlander Avatar asked Mar 28 '17 13:03

Josh Friedlander


1 Answers

Thanks to Ashish Nitin Patil for directing me to here.

I needed to transform the 'choices' into a dict, then get the value for key form.data, thus:

 value = dict(form.choice.choices).get(form.choice.data)
like image 84
Josh Friedlander Avatar answered Sep 30 '22 08:09

Josh Friedlander