Last active
December 23, 2022 23:04
-
-
Save mekza/516f172278c328468ea0 to your computer and use it in GitHub Desktop.
WTForms Select Field for country
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wtforms import SelectField | |
import pycountry | |
class CountrySelectField(SelectField): | |
def __init__(self, *args, **kwargs): | |
super(CountrySelectField, self).__init__(*args, **kwargs) | |
self.choices = [(country.alpha_2, country.name) for country in pycountry.countries] |
@dhernandezgt My snippet is a Field not a Form, you can find more info on Fields and Forms here https://wtforms.readthedocs.io/en/2.3.x/crash_course/
I created a package for this using your gist and also a StateSelectField that uses a similar approach.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to pass the form to HTLM page using Flask
the webpage.html
if I use self.choice or only choice the error es same in the:
jinja2.exceptions.UndefinedError: 'wtforms.fields.core.UnboundField object' has no attribute 'self'
orinja2.exceptions.UndefinedError: 'wtforms.fields.core.UnboundField object' has no attribute 'choice'
thanks