WTForms:如何呈现 HTML5 小部件?

WTForms: How to render an HTML5 Widget?

我正在尝试使用 WTForms 颜色输入字段。

我是这样定义表格的:

from wtforms.widgets.html5 import ColorInput

class ColoursForm(Form):
   background_color = ColorInput()

这是观点:

@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))

在我的 Jinja2 模板 (colours_edit.html) 中,我这样做:

<p> {{ form.background_color }} Pick a color here </p>

但是,它没有按预期呈现 HTML 颜色选择器,而是我在呈现的 HTML:

中看到了这个

<wtforms.widgets.html5.ColorInput object at 0x10b836e90> Pick a color here

为什么输入没有被渲染?

我解决了。我的代码有 2 个问题:

我在这里遗漏了 ():

<p> {{ form.background_color() }} Pick a color here </p>

表单应如下所示:

class ColoursForm(Form):
"""Used when editing scoreboard colours"""
   background_color = StringField(widget=ColorInput())

来自 .

最后,我不得不说 WTForms 文档在这方面不是很好。一些示例肯定会有所帮助。