我正在做一个flash消息,这样当用户注册时,它会显示一条消息“用户注册成功!”,但当我尝试登录时,它会显示一条消息“请登录以访问该页面”,即使我已经登录。我如何解决这个问题?
这是登录路线
@bp.route("user_settings/login", methods=["GET","POST"])
def login():
if current_user.is_authenticated:
return redirect(url_for('main.user_settings'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
return redirect(url_for("auth.login"))
login_user(user, remember=form.remember_me.data)
next_page = request.args.get("next")
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for("main.user_settings")
return redirect(next_page)
return render_template("auth/login.html", title='Log In', form=form)
这是注册路径
@bp.route("user_settings/register", methods=['GET', 'POST'])
@login_required
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(username=form.username.data, password_string=form.password.data, access_level=form.access_level.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
flash("User Successfully Registered!", "success")
return redirect(url_for('main.admin_mid', mode="admin"))
return render_template("auth/register.html", title="Register User", form = form)
HTML模板
{% with messages = get_flashed_messages() %} {% if messages %} {% for
message in messages %}
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
{% endfor %} {% endif %} {% endwith %}
1条答案
按热度按时间bmvo0sr51#
删除寄存器路由中的
@login_required
。应该是这样的。
注册途径:
@login_required
阻止用户进入注册页面,从而在未登录时创建帐户。