Im creating a form using flask, wtforms and jinja. Im trying to figure out how can my radiofields have pre-checked values based on data from db? For example I have a db of various merchants that need to be contacted. The fact that merchant has been contacted is shown in a radiobutton field:
contacted = RadioField('Has merchant been contacted', choices=[('No', 'No'), ('Yes', 'Yes')])
Whenever this form is saved, the value 'No' or 'Yes' gets passed as a string to table merchants
column contact
The value gets saved successfully, but each time the concrete merchants view is visited again the radiobutton is not checked with the previously saved value. This means that the apps user does not see the current status of the merchant.contact
column value (meaning he doesnt know if the merchant was or wasnt contacted)
I tried adding the merchant.contact
value in the value
attribute in jinja templates but it didnt work:
{{result_form.contacts(class='form-check-input', type='radio', value=merchant.contact)}}
Any ideas how to make the radiobutton field be checked based on a value from a database?
question from:https://stackoverflow.com/questions/66063083/flask-wtforms-radiofield-value-checked-based-on-data-from-database