Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask登录未重定向到上一页_Python_Redirect_Login_Flask_Flask Login - Fatal编程技术网

Python Flask登录未重定向到上一页

Python Flask登录未重定向到上一页,python,redirect,login,flask,flask-login,Python,Redirect,Login,Flask,Flask Login,我已经看到了很多关于这一点的问题,但还没有解决我的问题。我有一个Flask应用程序,可以登录Flask进行会话管理。而且,当我尝试在不登录的情况下查看页面时,会被重定向到/login/?next=%2Fsettings%2F 问题是,据我所知,“下一步”参数包含了我实际需要的站点部分,但在向登录表单提交请求时,它是通过POST完成的,因此我无法再使用此参数将其重定向到 我尝试使用Request.pathfrom(和url),但两者都只返回/login/作为请求url/path,而不是实际的/lo

我已经看到了很多关于这一点的问题,但还没有解决我的问题。我有一个Flask应用程序,可以登录Flask进行会话管理。而且,当我尝试在不登录的情况下查看页面时,会被重定向到
/login/?next=%2Fsettings%2F

问题是,据我所知,“下一步”参数包含了我实际需要的站点部分,但在向登录表单提交请求时,它是通过
POST
完成的,因此我无法再使用此参数将其重定向到

我尝试使用
Request.path
from(和url),但两者都只返回
/login/
作为请求url/path,而不是实际的
/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('/')
谢谢

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

下一个参数位于URL中的
之后,因此它是“查询字符串”的一部分。Flask已经为您解析了查询字符串中的项,您可以使用检索这些值。如果您向URL发送请求
/a/?b=c
,并且确实执行了
请求.args.get('b')
,您将收到
“c”

因此,您需要使用
request.args.get('next')
。文件

要记住的另一件事是,在HTML中创建登录表单时,不希望设置“action”属性。所以,不要这样做

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

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

您可以使用mongoengine会话通过flask会话传递“next\u URL”参数(
来自flask导入会话
)。在您定义应用程序和登录\u管理器的py文件中:

from flask.ext.mongoengine import MongoEngineSessionInterface
app.session_interface = MongoEngineSessionInterface(db)

@login_manager.unauthorized_handler
def unauthorized_callback():
    session['next_url'] = request.path
    return redirect('/login/')
然后在登录视图中:

def login():
    # ... if success
    next_url = session.get('next_url', '/')
    session.clear()
    return redirect(next_url)

正如我所说,我尝试了Request.url和类似的方法,路径就是一个例子。尽管如此,问题在于表单元素中的操作,我删除了它,按照文档的建议使用了参数,瞧,它现在起作用了。这太棒了,我真是太感谢你了!我甚至没有想到检查表单标签,其中
action=“/”
。嗯!:)非常感谢!谢谢你的回答,但我已经用接受的答案解决了这个问题。我的代码中有一个bug,但这在没有
mongoengine
的情况下是可以解决的,所以我就这么做了。我明白了,只是为了那些将来会遇到此类问题并使用mongo的人,我决定分享我的解决方案
def login():
    # ... if success
    next_url = session.get('next_url', '/')
    session.clear()
    return redirect(next_url)