Does anyone know of a way to create a datalist
field using WTForms in Flask?
I know how to create a SelectField
but I need to allow the user to enter their own value if it isn't in the list.
This is what I want to do http://www.w3schools.com/tags/tag_datalist.asp
Thanks
Class StringFieldA field that is indexed but not tokenized: the entire String value is indexed as a single token. For example this might be used for a 'country' field or an 'id' field, or any field that you intend to use for sorting or access through the field cache.
Flask WTForms is a library that makes form handling easy and structured. It also ensures the effective handling of form rendering, validation, and security. To build forms with this approach, you start by creating a new file in our app directory and name it forms.py. This file will contain all the application forms.
Step 1 — Installing Flask and Flask-WTF In this step, you'll install Flask and Flask-WTF, which also installs the WTForms library automatically. With your virtual environment activated, use pip to install Flask and Flask-WTF: pip install Flask Flask-WTF.
You create a simple StringField in your view.
autocomplete_input = StringField('autocomplete_input', validators=[DataRequired()])
In your template you call the field and add the list parameter (remember to pass the entries to your template):
{{form.autocomplete_input(list="id_datalist")}}
<datalist id="id_datalist">
{% for entry in entries %}
<option value={{ entry }}>
{% endfor %}
</datalist>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With