烧瓶登录未重定向到上一页 [英] Flask-login not redirecting to previous page

查看:132
本文介绍了烧瓶登录未重定向到上一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到这一点,我已经看到了很多问题,但是还不能解决我的问题.我有一个带有烧瓶登录的Flask应用程序,用于会话管理.而且,当我尝试不登录而查看页面时,我被重定向到/login/?next=%2Fsettings%2F

I have seen quite a few questions with this in mind, but haven't been able to address my issue. I have a Flask app with flask-login for session management. And, when I try to view a page without logging in, I get redirected to a link in form of /login/?next=%2Fsettings%2F

据我所知,问题是下一个"参数保留了我实际需要的网站部分,但是在向登录表单提交请求时,它是通过POST完成的,因此,该参数不再可供我重定向到该参数.

The issue is, as far as I could have it understand, that the "next" argument holds the part of the site I actually need, but when submitting a request to a login form, it is done via POST, so this argument is no longer available for me to redirect it to.

我尝试使用请求(和url),但都只是返回/login/作为请求url/路径,而不是实际的/login/?next=xxx.

I tried using Request.path from Request (and url) but both just return the /login/ as the request url/path, not the actual /login/?next=xxx.

我的登录方法如下:

@app.route('/login/', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        #getting the user
        user = User.get(request.form['username'])
        if user.user is None:
            return redirect('/login/')
        #actual login proces
        if user and check_password_hash(user.user.password, request.form['password']):
            login_user(user, remember=remember)
            #the redirection portion of the login process
            return redirect(request.path or ("/")) # I tried various options there but without success, like request.args['next'] and such

        return redirect('/login/')

    else:
        return redirect('/')

谢谢

推荐答案

request.path 不是您想要的.它返回URL的实际路径.因此,如果您的URL是/a/?b=c,则request.path返回/a,而不是您期望的c.

request.path is not what you're looking for. It returns the actual path of the URL. So, if your URL is /a/?b=c, then request.path returns /a, not c as you are expecting.

next参数在URL中的?之后,因此它是查询字符串"的一部分. Flask已经为您解析了查询字符串中的项目,您可以使用 .如果您向URL /a/?b=c发送请求并执行request.args.get('b'),则会收到"c".

The next parameter is after the ? in the URL, thus it is part of the "query string". Flask has already parsed out items in the query string for you, and you can retrieve these values by using request.args. If you sent a request to the URL /a/?b=c and did request.args.get('b'), you would receive "c".

因此,您想使用request.args.get('next').文档在示例中显示了其工作原理.

So, you want to use request.args.get('next'). The documentation shows how this works in an example.

要记住的另一件事是,当您使用HTML创建登录表单时,您不想设置"action"属性.所以,不要这样做.

Another thing to keep in mind is that when you are creating your login form in HTML, you don't want to set the "action" attribute. So, don't do this..

<form method="POST" action="/login">
    ...
</form>

这将导致对/login而不是/login/?next=%2Fsettings%2F发出POST请求,这意味着您的next参数将不属于查询字符串,因此您将无法检索它.您想取消"action"属性:

This will cause the POST request to be made to /login, not /login/?next=%2Fsettings%2F, meaning your next parameter will not be part of the query string, and thus you won't be able to retrieve it. You want to leave off the "action" attribute:

<form method="POST">
    ...
</form>

这将导致表单发布到当前URL(应为/login/?next=%2Fsettings%2f).

This will cause the form to be posted to the current URL (which should be /login/?next=%2Fsettings%2f).

这篇关于烧瓶登录未重定向到上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆