flask_login会自动设置"next"参数吗?



我使用Flask Login创建了一个登录和注销页面。我正在尝试在没有先登录的情况下导航到注销页面,但我不断被重定向到这里:http://127.0.0.1:5000/login?next=%2Findex

这是我的注销功能:

from flask import render_template, flash, redirect, url_for, request
from app import app
from app.forms import LoginForm, RegistrationForm
from flask_login import current_user, login_user, logout_user, login_required
from app.models import User
from app import db
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))

您正在重定向到需要用户登录的视图,因此设置了next=值。

@app.route('/')
@login_required # <-- Your index view has this.
def index():
return 'Index'

将用户重定向到不需要登录或注销时不需要@login_required装饰器的页面。

当未登录的用户访问受@login_required装饰器保护的视图功能时,装饰器将重定向到登录页面,但它将在此重定向中包含一些额外的信息,以便应用程序可以返回到第一页。例如,如果用户导航到/index,@login_required装饰器将截获请求并响应重定向到/login,但它会向此 URL 添加查询字符串参数,从而生成完整的重定向 URL/login?next=/index。下一个查询字符串参数设置为原始 URL,因此应用程序可以使用它来在登录后重定向回来。

检查:

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins

最新更新