提交表单时页面未重定向
Page not redirecting when submitting the form
我正在创建一个博客应用程序。我使用 post 方法创建了一个注册表单,要求提供详细信息。在烧瓶路线中,我试图检查我的表格是否正确验证。为此,我创建了一个 if 语句,当条件为真时,我尝试重定向到主路由。这没有发生。我不确定这里有什么问题。但是我可以看到 post 给出了 200 状态码。
我试图打印一些东西,但它没有打印任何东西。我试图从 html 文件中删除该方法,我尝试重做注册路由我的猜测是验证本身没有发生。
#home route
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html', posts=posts, title="Home")
# register route
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
#flash(f'Account created for {form.username.data}!', 'success')
print("Account creation success")
return redirect(url_for('home')) # function name for the route.
return render_template('register.html', title="Register", form=form)
# forms.py file
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired,length, Email,EqualTo
class RegistrationForm(FlaskForm):
username= StringField('Username',validators=[
DataRequired(),
length(min=2, max=20)
])
email= StringField('Email',validators=[
DataRequired(), Email()
])
password= PasswordField( 'Password', validators=[DataRequired()])
confirmPassword = PasswordField('Confirm Password', validators=[DataRequired(),EqualTo(password)])
submit= SubmitField("Sign Up")
<!--This is layout.html which is parent for the home page-->
<div class="col-md-8">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{category}}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith%}
{% block content %}{% endblock content%}
</div>
<!--This is register.html which will render the form -->
{% extends "layout.html" %}
{% block content %}
<div class="content-section">
<form method="POST" action="">
<!--adds cross site request forgery token. Needed for secuity.-->
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<div class="form-group">
{{ form.username.label(class="form-control-label") }}
{{ form.username(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.email.label(class="form-control-label") }}
{{ form.email(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.password.label(class="form-control-label") }}
{{ form.password(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.confirmPassword.label(class="form-control-label") }}
{{ form.confirmPassword(class="form-control form-control-lg") }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
<div class="border-top pt-3">
<small class="text-muted">
<!-- url_for() uses the name of the route function -->
Already Have an account? <a class="ml-2" href="{{ url_for('register') }}" >Sign In</a>
</small>
</div>
{% endblock content %}
预期的输出是页面必须重定向到 home.html 但由于某种原因,它停留在 register.html 并且没有错误消息。我什至得到了 post 操作的 200 状态码。
发布表单后出现错误:
{'confirmPassword': ["Invalid field name '<UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.DataRequired object at 0x7fecdc7b6e80>]})>'."]}
这是因为您将 password
字段实例传递给了 EqualTo
验证器,而不是将其名称作为字符串传递:'password'
.
confirmPassword = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])
我正在创建一个博客应用程序。我使用 post 方法创建了一个注册表单,要求提供详细信息。在烧瓶路线中,我试图检查我的表格是否正确验证。为此,我创建了一个 if 语句,当条件为真时,我尝试重定向到主路由。这没有发生。我不确定这里有什么问题。但是我可以看到 post 给出了 200 状态码。
我试图打印一些东西,但它没有打印任何东西。我试图从 html 文件中删除该方法,我尝试重做注册路由我的猜测是验证本身没有发生。
#home route
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html', posts=posts, title="Home")
# register route
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
#flash(f'Account created for {form.username.data}!', 'success')
print("Account creation success")
return redirect(url_for('home')) # function name for the route.
return render_template('register.html', title="Register", form=form)
# forms.py file
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired,length, Email,EqualTo
class RegistrationForm(FlaskForm):
username= StringField('Username',validators=[
DataRequired(),
length(min=2, max=20)
])
email= StringField('Email',validators=[
DataRequired(), Email()
])
password= PasswordField( 'Password', validators=[DataRequired()])
confirmPassword = PasswordField('Confirm Password', validators=[DataRequired(),EqualTo(password)])
submit= SubmitField("Sign Up")
<!--This is layout.html which is parent for the home page-->
<div class="col-md-8">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{category}}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith%}
{% block content %}{% endblock content%}
</div>
<!--This is register.html which will render the form -->
{% extends "layout.html" %}
{% block content %}
<div class="content-section">
<form method="POST" action="">
<!--adds cross site request forgery token. Needed for secuity.-->
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<div class="form-group">
{{ form.username.label(class="form-control-label") }}
{{ form.username(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.email.label(class="form-control-label") }}
{{ form.email(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.password.label(class="form-control-label") }}
{{ form.password(class="form-control form-control-lg") }}
</div>
<div class="form-group">
{{ form.confirmPassword.label(class="form-control-label") }}
{{ form.confirmPassword(class="form-control form-control-lg") }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
<div class="border-top pt-3">
<small class="text-muted">
<!-- url_for() uses the name of the route function -->
Already Have an account? <a class="ml-2" href="{{ url_for('register') }}" >Sign In</a>
</small>
</div>
{% endblock content %}
预期的输出是页面必须重定向到 home.html 但由于某种原因,它停留在 register.html 并且没有错误消息。我什至得到了 post 操作的 200 状态码。
发布表单后出现错误:
{'confirmPassword': ["Invalid field name '<UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.DataRequired object at 0x7fecdc7b6e80>]})>'."]}
这是因为您将 password
字段实例传递给了 EqualTo
验证器,而不是将其名称作为字符串传递:'password'
.
confirmPassword = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])