我在行中有TypeError,我在表单上调用'validate()'。
错误是:
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)
我的代码:
@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)
这有点奇怪,因为我在之前的控制器中使用它,它没有任何参数。
请帮我解决。
答案 0 :(得分:16)
您需要实例化验证器:
org_name = TextField(u'Название организации', [validators.Required()])
请注意()
之后的Required
;这适用于所有验证器。然后再次调用该实例以验证输入。