How to pass parameters on onChange of html select with flask-wtf
10,196
It can be done by add javascript onchange function as attribute
<form method=post action="/register">
{{ render_field(form.country(**{"onchange":"this.form.submit()"})) }}
<input type=submit value=Register>
</form>
Author by
Nickpick
Pokerbots, neural networks, artificial intelligence, kite surfing, piano playing
Updated on June 11, 2022Comments
-
Nickpick almost 2 years
The following flask code creates a select .. option dropdown menu:
model:
class SelectForm(Form): country = SelectField('Country', choices=[ ('us','USA'),('gb','Great Britain'),('ru','Russia')])
flask app:
@app.route('/new') def new(): form = SelectForm() return render_template('new.html', form = form )
html file:
<form method=post action="/register"> {{ render_field(form.country) }} <p><input type=submit value=Register> </form>
macro file the defines render_field:
{% macro render_field(field) %} <dt>{{ field.label }} <dd>{{ field(**kwargs)|safe }} {% if field.errors %} <ul class=errors> {% for error in field.errors %} <li>{{ error }}</li> {% endfor %} </ul> {% endif %} </dd> {% endmacro %}
What is the best way to have onchange submit the results automatically without the user having to press the submit button? Is there a way to change the macro or what's the most elegant way?
thanks
-
Ben over 3 yearsWorks with the render_kw setting as follows... render_kw={"onchange":"this.form.submit()"})