gpt4 book ai didi

python - 提交表单后,如何解决validate_on_submit()函数引发的异常

转载 作者:行者123 更新时间:2023-12-03 16:56:42 25 4
gpt4 key购买 nike

这是

我遵循了很多线程,这些问题表明问题代码存在于表单类的验证器中,但是我已经检查了它们。.此处为fo中的类代码

1.这是来自forms.py的代码

from flask_wtf import Form
from wtforms import StringField, SubmitField, BooleanField, PasswordField
from wtforms.validators import DataRequired, Length, email, EqualTo, Required


class RegistrationForm(Form):
username = StringField('username', validators=[DataRequired(),
Length(min=2, max=20)])
email = StringField('email', validators=[DataRequired(), email])
password = PasswordField('password', validators=[DataRequired()])
conf_password = PasswordField('conf_pass', validators=[DataRequired(),
EqualTo('password', message='passwords must match')])
submit = SubmitField('sign up')


2.这是模板文件中的代码

  {% extends 'layout.html' %}
{% block content %}
<div class="card" width="300px">
<form method="post" action="">
{{ form.hidden_tag() }}
<feildset>
<legend>
join Today
</legend>
</feildset>
<hr>
{{ form.username.label }}
{{ form.username }}
{{ form.email.label }}
{{ form.email }}
{{ form.password.label }}
{{ form.password }}
{{ form.conf_password.label }}
{{ form.conf_password() }}
{{ form.submit(class='success button centering') }}
</form>
</div>
{% endblock %}


3.这是来自app.py的代码

@app.route('/reg', methods=['GET', 'POST'])
def show_reg():
form = RegistrationForm()
if form.validate_on_submit():
flash(f'Account created for {form.username.data}!', 'success')
return redirect(url_for('home'))
return render_template('register.html', title='register', form=form)


3. stacktrace:

Traceback (most recent call last):
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/PycharmProjects/site/app.py", line 37, in show_reg
if form.validate_on_submit():
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/flask_wtf/form.py", line 101, in validate_on_submit
return self.is_submitted() and self.validate()
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/wtforms/form.py", line 310, in validate
return super(Form, self).validate(extra)
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/wtforms/form.py", line 152, in validate
if not field.validate(self, extra):
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/wtforms/fields/core.py", line 206, in validate
stop_validation = self._run_validation_chain(form, chain)
File "/root/PycharmProjects/site/venv/lib/python3.6/site-packages/wtforms/fields/core.py", line 226, in _run_validation_chain
validator(form, self)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given

最佳答案

最好的机会是,问题出在email验证器中:wtforms期望一个类实例,但是您已经传递了一个类,因此,当wtforms执行validator(form, self)时,它们将调用类的init方法,而不是实例的调用方法。更改为validators=[DataRequired(), email()]应该可以解决。

关于python - 提交表单后,如何解决validate_on_submit()函数引发的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53908336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com