WTForms Form.validate(self)始终返回false

时间:2016-03-23 04:33:41

标签: python wtforms

我正在创建一个表单,但出于某种原因,每当我尝试验证它时,它总是返回False。

形式:

class AddStuffForm(Form):
    myList = [(idx,int(val.tier)) for idx,val in enumerate(getList())]
    myList.remove((2,3)) # this is an element I don't want in the list

    myListForm = SelectField("List:", [Required()], choices=myList)

    def __init__(self, *args, **kwargs):
        """ Constructor. """
        Form.__init__(self, *args, **kwargs)
        self.user = None

    def validate(self):
        """ Validates registration form.  """
        rv = Form.validate(self)
        if not rv:
            return False

查看:

@app.route('/addStuff', methods=['GET', 'POST'])
def addStuff():
    """ Submits register form. """

    form = AddStuffForm()
    if form.validate_on_submit():
        return redirect(url_for('addStuff'))

    return render_template('/AddStuff.html', form=form)

AddStuff.html:

{% extends "layout.html" %}

{% block content %}

<h1>Add stuff</h1>
<form method="POST" action="{{ url_for('addStuff') }}">
    {{ form.hidden_tag() }}
    <p>{{ form.myListForm.label }} {{ form.myListForm(size=1) }}</p>

    <input type="submit" value="Add it">
</form>
{% endblock %}

在查找问题时,我唯一能找到的就是确保“{{form.hidden_​​tag()}}”在html中,但正如您所看到的,我已经拥有了。我错过的SelectField是否有错误?

1 个答案:

答案 0 :(得分:0)

决定只删除&#34;如果不是rv:返回False&#34;位。如果背景中出现任何问题,那么哦。