3

I'm working on a project using Flask and flask-wtforms, and I'm facing a problem setting selected value for Selectfield in Jinja: I tried the following but nothing worked...

@app.route('/route', methods = ['GET','POST']
def route():
   form = InputForm(request.form)
   # Data base connection code returns connection as con and cursor as cur.
   cur.execute('SELECT field FROM Table WHERE Row = %s', (Rowvar,))
   Data = cur.fetchall()
   con.close()
   return render_template('template.html', form = form, Data = Data)
   

the HTMLtemplates I tried: Try1:

<html>
...
{% for i in Data %}
    {{form.selectfield.default = i[0]}}
{% endfor %}
...
</html>

Try 2:

<html>
...
{% for i in Data %}
    {% form.selectfield(default = i[0]) %}
{% endfor %}
...
</html>

Try 3 using render_field function:

<html>
...
{% for i in Data %}
    {{render_field(form.selectfield, default = i[0])}}
{% endfor %}
...
</html>

I also tried using render field with (value) and (select) instead of default and none of them actually worked.

So.. can you please help me on how to set a SelectField value using Jinja the right way.

Thank you very much.

  • see the answer to this [question](https://stackoverflow.com/questions/54823048/select-default-value-for-flask-wtforms-selectfield-within-jinja-for-dynamic-data/57672931#57672931). – Geo_Python Sep 02 '19 at 19:04

0 Answers0