最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

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

Flask crifan 965浏览 0评论

折腾:

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

期间,结果用代码:

@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))

却出错:

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:830]:
after wechat auth, redirect to nextRedirectUrl=http://hd.webonn.com/show_event/event-530a2508-5c33-445c-8839-3b6e40daded4?from=singlemessage&isappinstalled=0

<div–<——————————————————————————

[2016-09-10 18:36:20 +0000] [4504] [ERROR] Error handling request /wechat_auth?nextRedirectUrl=http%3A//hd.webonn.com/show_event/event-530a2508-5c33-445c-8839-3b6e40daded4%3Ffrom%3Dsinglemessage%26isappinstalled%3D0&code=011Butrt1ScEl80HjIst1j6urt1ButrY&state=
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle
    self.handle_request(listener, req, client, addr)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File “/root/html/SIPEvents/sipevents/views.py”, line 837, in wechat_auth
    return redirect(url_for(nextRedirectUrl))
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/helpers.py”, line 332, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1811, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/helpers.py”, line 322, in url_for
    force_external=external)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/werkzeug/routing.py”, line 1758, in build
    raise BuildError(endpoint, values, method, self)
BuildError: Could not build url for endpoint u’http://hd.webonn.com/show_event/event-530a2508-5c33-445c-8839-3b6e40daded4?from=singlemessage&isappinstalled=0′. Did you mean ‘show_event’ instead?

flask redirect BuildError: Could not build url for endpoint

python – Getting werkzeug.routing.BuildError – Stack Overflow

flask url_for    Could not build url for endpoint

再次出错:

after wechat auth, redirect to nextRedirectUrl=http://hd.webonn.com/

<div–<——————————————————————————

[2016-09-10 18:57:00 +0000] [4836] [ERROR] Error handling request /wechat_auth?nextRedirectUrl=http%3A//hd.webonn.com/&code=021KxXRr09Fr5s1Mz1Rr0QlTRr0KxXR4&state=
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle
    self.handle_request(listener, req, client, addr)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File “/root/html/SIPEvents/sipevents/views.py”, line 836, in wechat_auth
    return redirect(url_for(nextRedirectUrl))
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/helpers.py”, line 332, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1811, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/helpers.py”, line 322, in url_for
    force_external=external)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/werkzeug/routing.py”, line 1758, in build
    raise BuildError(endpoint, values, method, self)
BuildError: Could not build url for endpoint u’http://hd.webonn.com/’. Did you mean ‘static’ instead?

想到了一个:

能不能在redirect和url_for时把当前参数传递过去?

所以去尝试解决:

如何在Flask的redirect的url_for时把当前request的参数都传递过去

flask login_required for POST with redirect

最后发现是:

return redirect(url_for(nextRedirectUrl))

之前写的是:

return redirect(nextRedirectUrl)

然后就可以正常redirect了。

转载请注明:在路上 » [已解决]Flask中redirect时出错:BuildError Could not build url for endpoint Did you mean show_event instead

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
62 queries in 0.245 seconds, using 12.17MB memory