FlaskLogin | url查询字符串arg'next'

2024-10-02 00:38:02 发布

您现在位置:Python中文网/ 问答频道 /正文

验证蓝图登录路径:

@auth.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
    doctor_id = request.form.get('doctor-id')
    password = request.form.get('password')
    docktor = session.query(Doctor).filter_by(doctor_health_id=int(doctor_id)).first()

    if docktor:
        if docktor.verify_password(password):
            login_user(user=docktor)
            flash("Login Successful", "ok")
            login_user(docktor)

            _next = request.args.get('next')

            print(_next)
            if _next is None or not _next.startswith('/'):
                _next = url_for("medic.home")
                print(_next)
                return redirect(url_for('medic.home'))

        else:
            flash('wrong password')
            return redirect(url_for('auth.login'))
    else:
        flash('Wrong credentials')
        return url_for('auth.login')

else:
    return render_template('auth/login.html')

医疗蓝图回家路线:

@medic.route('/')
@login_required
def home():
    return render_template('medic/home.html')

所以我的问题是,我尝试使用Flask登录来登录用户。。。。但我无法解决登录路径发送给我的问题

http://127.0.0.1:5000/auth/login?next=%2Fmedic%2F

此url

我不知道我做错了什么

我阅读了Flask和Flask登录的文档,并在stackoverflow上搜索,但我资助的解决方案对我来说不起作用。。 如何解决查询字符串问题

请求日志为:

127.0.0.1 - - [13/Feb/2021 11:18:37] "POST /auth/login HTTP/1.1" 302 -
127.0.0.1 - - [13/Feb/2021 11:18:37] "GET /medic/ HTTP/1.1" 302 -
127.0.0.1 - - [13/Feb/2021 11:18:37] "GET /auth/login?next=%2Fmedic%2F             
HTTP/1.1" 200 -`

Tags: authidurlhomeforgetreturnif
1条回答
网友
1楼 · 发布于 2024-10-02 00:38:02

对于_nextstartswith“/”的情况,您需要一个else语句 在您的情况下_next以“/”开头(或为None),您给定的代码转到底部的返回:

return render_template('auth/login.html')

在url中使用现有的_next作为arg,这就是它将您发送到的原因

http://127.0.0.1:5000/auth/login?next=%2Fmedic%2F

因此,请使用else语句完成代码的以下部分,否则,每次不满足当前条件时,它都会在底部的return语句(return render_template('auth/login.html'))处发送给您:

if _next is None or not _next.startswith('/'):
                _next = url_for("medic.home")
                print(_next)
                return redirect(url_for('medic.home'))

相关问题 更多 >

    热门问题