问题描述
我正在尝试使用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中看到了这一点:
选择一种颜色 在这里
为什么不呈现输入?
解决方法
我解决了。我的代码有2个问题:
我在这里不见了():
<p> {{ form.background_color() }} Pick a color here </p>
该表单应如下所示:
class ColoursForm(Form):
"""Used when editing scoreboard colours"""
background_color = StringField(widget=ColorInput())
最后,我不得不说WTForms文档在此方面不是很好。一些例子肯定会有所帮助。