简体   繁体   English

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

[英]WTForms Form.validate(self) always returns false

I am creating a form, but for some reason whenever I try and validate it, it always returns False. 我正在创建一个表单,但是由于某种原因,每当我尝试验证它时,它总是返回False。

Form: 形成:

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

Views: 浏览次数:

@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: 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 %}

When looking up the problem, the only thing I have been able to find says to make sure "{{ form.hidden_tag() }}" is in the html, but as you can see, I already have that. 在查找问题时,我唯一能找到的就是确保“{{form.hidden_​​tag()}}”在html中,但正如您所看到的,我已经拥有了。 Is there some error with the SelectField I am missing? 我缺少的SelectField是否存在一些错误?

Decided to just delete the "If not rv: return False" bit. 决定只删除“If not rv:return False”位。 If anything wrong is going on in the background, oh well. 如果在后台发生任何问题,那么哦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM