Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create a datalist field in Flask WTForms?

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

like image 772
Lucas Amos Avatar asked Jan 27 '16 22:01

Lucas Amos


People also ask

What is StringField?

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.

What is WTForms flask?

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.

How do I import WTForms into a flask?

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.


1 Answers

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>
like image 157
limlug Avatar answered Sep 19 '22 03:09

limlug