Saturday, 29 August 2020

WTForms: how do I set a default value for an HTML5 Widget?

I'm trying to use the WTForms color input field.

How can I set a default value (e.g. #ff0000) for the input field?

This is how I define the form:

from wtforms.widgets.html5 import ColorInput

class ColoursForm(Form):
   background_color = StringField(widget=ColorInput())

This is the view:

@app.route("/colours/<token>/", methods=['GET', 'POST'])
def edit_colours(token):
   form = ColoursForm(request.form)
   if request.method == 'GET':
      return render_template('colours_edit.html', form=form, token=token)
   else:  # Request = post
      return redirect(url_for('view_scoreboard', token=token))

In my Jinja2 Template (colours_edit.html) I do this:

<p>  Pick a color here </p>

It all works, but I don't know how to set a default value. What doesn't work is this in the form:

background_color = StringField(widget=ColorInput(), default="#ff00ff")


from WTForms: how do I set a default value for an HTML5 Widget?

No comments:

Post a Comment