Type error on calling validate in wtforms

廉价感情. 提交于 2019-12-01 02:27:28

问题


I have TypeError in line where I call 'validate()' on my form.

The error is:

   Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/valery/projects/easy_booking/easy_booking/controllers/users.py", line 110, in process_field
    if filled_form.validate():
  File "/usr/local/lib/python2.7/dist-packages/wtforms/form.py", line 265, in validate
    return super(Form, self).validate(extra)
  File "/usr/local/lib/python2.7/dist-packages/wtforms/form.py", line 130, in validate
    if not field.validate(self, extra):
  File "/usr/local/lib/python2.7/dist-packages/wtforms/fields/core.py", line 176, in validate
    stop_validation = self._run_validation_chain(form, chain)
  File "/usr/local/lib/python2.7/dist-packages/wtforms/fields/core.py", line 196, in _run_validation_chain
    validator(form, self)
TypeError: __init__() takes at most 2 arguments (3 given)

My code:

@app.route("/process_field", methods=['POST'])
def process_field():
    file = request.files['upload']
    filled_form = FieldFootball(request.form)
    if filled_form.validate():
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOADS_FOLDER'], filename))
        doc = {"organization": filled_form.org_name.data, "address": filled_form.address.data,
               "filename": filename, "price": filled_form.price.data, "phone": filled_form.phone.data,
               "phone1": filled_form.phone1.data, "phone2": filled_form.phone2.data, "user_id": current_user.dic['_id']}
        field = Field(doc)
        field.save()
        return redirect(url_for('field_profile', id=field.id))
    else:
        return render_template("add_field.html", form=filled_form)

class FieldFootball(Form):
    org_name = TextField(u'Название организации', [validators.Required])
    address = TextAreaField(u'Полный адрес футбольного поля',[validators.Required])
    upload = FileField(u'Фотографии футбольного поля', validators=[
        FileRequired(),
        FileAllowed(['jpg', 'png'], u'Только фотографии!')
    ])
    price = TextField(u'Цена', [validators.Required, validators.NumberRange])
    phone = TextField(u'Телефоны', [validators.Required])
    phone1 = TextField(u'')
    phone2 = TextField(u'')
    my_choices = [('1', u'Трава'), ('2', u'Крытый'), ('3', u'Трибуны')]
    list_tags = MultiCheckboxField(choices = my_choices)

It is a bit weird because I used it in previous controller, and it goes without any argument.

Please help me solve it.


回答1:


You need to instantiate your validators:

org_name = TextField(u'Название организации', [validators.Required()])

Note the () after Required; this applies to all your validators. The instance is then called again to validate the input.



来源:https://stackoverflow.com/questions/18812240/type-error-on-calling-validate-in-wtforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!