为什么我的 wtforms 错误处理不起作用?
Why is my wtforms error handling not working?
我正在尝试构建一个 flask 登录和注册系统。一切正常,除非用户向表单提供了不正确的输入,否则会出现错误,因为还没有工作错误处理。我正在使用 wtforms,这是注册表单 class:
class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)])
email = StringField('Email', validators=[DataRequired(),Email()])
password = PasswordField('Password', validators=[DataRequired(), Length(min=4)])
confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), Length(min=4), EqualTo('password')])
submit = SubmitField('Sign Up')
这是报名路线
@app.route('/register', methods=["GET", "POST"])
def register():
form = RegistrationForm()
if request.method == "POST":
if form.validate_on_submit():
return redirect(url_for('home'))
else:
return render_template('register.html', form=form)
我已经厌倦了处理 html 中的错误,但每次用户输入不正确时,它都会显示此错误消息“TypeError:'register' 的视图函数没有 return 一个有效的响应。该函数要么 returned None 要么在没有 return 语句的情况下结束。”这是我的 html 表格:
<form action="/register" method="POST">
<h1>Create Account</h1>
{{ form.hidden_tag() }} {% if form.username.errors %} {{
form.username(placeholder="Username", class="is-invalid") }} {% for
error in form.username.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.username(placeholder="Username") }} {%
endif %} {% if form.email.errors %} {{ form.email(placeholder="Email",
class="is-invalid") }} {% for error in form.email.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.email(placeholder="Email") }} {% endif
%} {% if form.password.errors %} {{
form.password(placeholder="Password", class="is-invalid") }} {% for
error in form.password.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.password(placeholder="Password") }} {%
endif %} {% if form.confirm_password.errors %} {{
form.confirm_password(placeholder="Confirm Password",
class="is-invalid") }} {% for error in form.confirm_password.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.confirm_password(placeholder="Confirm
Password") }} {% endif %}
<div>{{ form.submit(class="signup") }}</div>
</form>
如有任何回应或帮助,我们将不胜感激。谢谢!
每个视图都必须 return 一些东西。如果方法是 post 但未验证,则您的 return 什么也不是。逻辑问题,与wtforms无关
if request.method == "POST":
if form.validate_on_submit():
return redirect(url_for('home'))
else:
return 'oops'
else:
return render_template('register.html', form=form)
我正在尝试构建一个 flask 登录和注册系统。一切正常,除非用户向表单提供了不正确的输入,否则会出现错误,因为还没有工作错误处理。我正在使用 wtforms,这是注册表单 class:
class RegistrationForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)])
email = StringField('Email', validators=[DataRequired(),Email()])
password = PasswordField('Password', validators=[DataRequired(), Length(min=4)])
confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), Length(min=4), EqualTo('password')])
submit = SubmitField('Sign Up')
这是报名路线
@app.route('/register', methods=["GET", "POST"])
def register():
form = RegistrationForm()
if request.method == "POST":
if form.validate_on_submit():
return redirect(url_for('home'))
else:
return render_template('register.html', form=form)
我已经厌倦了处理 html 中的错误,但每次用户输入不正确时,它都会显示此错误消息“TypeError:'register' 的视图函数没有 return 一个有效的响应。该函数要么 returned None 要么在没有 return 语句的情况下结束。”这是我的 html 表格:
<form action="/register" method="POST">
<h1>Create Account</h1>
{{ form.hidden_tag() }} {% if form.username.errors %} {{
form.username(placeholder="Username", class="is-invalid") }} {% for
error in form.username.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.username(placeholder="Username") }} {%
endif %} {% if form.email.errors %} {{ form.email(placeholder="Email",
class="is-invalid") }} {% for error in form.email.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.email(placeholder="Email") }} {% endif
%} {% if form.password.errors %} {{
form.password(placeholder="Password", class="is-invalid") }} {% for
error in form.password.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.password(placeholder="Password") }} {%
endif %} {% if form.confirm_password.errors %} {{
form.confirm_password(placeholder="Confirm Password",
class="is-invalid") }} {% for error in form.confirm_password.errors %}
<span>{{ error }}</span>
{% endfor %} {% else %} {{ form.confirm_password(placeholder="Confirm
Password") }} {% endif %}
<div>{{ form.submit(class="signup") }}</div>
</form>
如有任何回应或帮助,我们将不胜感激。谢谢!
每个视图都必须 return 一些东西。如果方法是 post 但未验证,则您的 return 什么也不是。逻辑问题,与wtforms无关
if request.method == "POST":
if form.validate_on_submit():
return redirect(url_for('home'))
else:
return 'oops'
else:
return render_template('register.html', form=form)