最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[未解决]Flask中login_required和redirect之后如何保持之前的POST而不是GET

Flask crifan 1330浏览 0评论

Flask中代码:

@app.route(“/login”)
def login():
    requestArgs = request.args
    gLog.debug(‘requestArgs=%s’, requestArgs)
    # requestArgs=ImmutableMultiDict([(‘next’, u’http://hd.webonn.com/’)])
    nextRedirectUrl = requestArgs.get(“next”, “”)
    gLog.debug(“nextRedirectUrl=%s”, nextRedirectUrl)
    return redirect(url_for(“wechat_auth”, nextRedirectUrl=nextRedirectUrl))
def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            return redirect(url_for(‘login’, next=request.url))
        return f(*args, **kwargs)
    return decorated_function
@app.route(“/wechat_auth”)
def wechat_auth():
        if nextRedirectUrl:
            gLog.debug(“after wechat auth, redirect to nextRedirectUrl=%s”, nextRedirectUrl)
            # after wechat auth, redirect to nextRedirectUrl=http://hd.webonn.com/
            return redirect(nextRedirectUrl)
@app.route(‘/join_event/<event_id>’, methods=[‘POST’])
@login_required
def join_event(event_id):

现在的问题是:

当访问:

http://hd.webonn.com/join_event/event-6c316a40-1f14-495a-a7af-80c2febe31df

时,调用到:join_event是POST

但是需要去判断是否登陆:login_required

其中会掉转到login-》wechat_auth

微信授权登录后,再redirect回来

但是等回来的时候的url已经变成了GET了:

导致页面无法访问:

Method Not Allowed
The method is not allowed for the requested URL.

flask redirect post

python – Make a POST request while redirecting in flask – Stack Overflow

python – How to redirect to an external url with parameters and POST method? – Stack Overflow

然后去改为:

@app.route(“/login”, methods=[‘GET’, ‘POST’])
def login():
    requestArgs = request.args
    gLog.debug(‘requestArgs=%s’, requestArgs)
    # requestArgs=ImmutableMultiDict([(‘next’, u’http://hd.webonn.com/’)])
    nextRedirectUrl = requestArgs.get(“next”, “”)
    gLog.debug(“nextRedirectUrl=%s”, nextRedirectUrl)
    requestMethod = request.method
    gLog.debug(“requestMethod=%s”, requestMethod)
    if requestMethod == “POST”:
        gLog.debug(“redirect to wechat_auth using code 307 for %s”, nextRedirectUrl)
        return redirect(url_for(“wechat_auth”, nextRedirectUrl=nextRedirectUrl), code=307)
    else:
        return redirect(url_for(“wechat_auth”, nextRedirectUrl=nextRedirectUrl))
@app.route(“/wechat_auth”, methods=[‘GET’, ‘POST’])
# @app.route(“/wechat_auth”)
def wechat_auth():
        if nextRedirectUrl:
            gLog.debug(“after wechat auth, redirect to nextRedirectUrl=%s”, nextRedirectUrl)
            # after wechat auth, redirect to nextRedirectUrl=http://hd.webonn.com/
            if requestMethod == “POST”:
                gLog.debug(“redirect using code 307 for %s”, nextRedirectUrl)
                return redirect(url_for(nextRedirectUrl), code=307)
            else:
                return redirect(url_for(nextRedirectUrl))
        else:
            gLog.debug(“after wechat auth, redirect to default router: index”)
            return redirect(url_for(“index”))
def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            requestMethod = request.method
            requestArgs = request.args
            gLog.debug(‘when g.user is None, requestMethod=%s, requestArgs=%s’, requestMethod, requestArgs)
            if requestMethod == “POST”:
                gLog.debug(“redirect to login using code 307 for %s”, request.url)
                return redirect(url_for(‘login’, next=request.url), code=307)
            else:
                return redirect(url_for(‘login’, next=request.url))
        return f(*args, **kwargs)
    return decorated_function

然后又出错:

[已解决]Flask中redirect时出错:BuildError Could not build url for endpoint Did you mean show_event instead

最后,其实还是没有完全解决此处的问题:此处因为涉及到多次的跳转

转载请注明:在路上 » [未解决]Flask中login_required和redirect之后如何保持之前的POST而不是GET

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
64 queries in 0.108 seconds, using 18.85MB memory