尝试使用url_for重定向时werkzeug.routing.BuildError

时间:2014-06-03 18:24:22

标签: flask

这是导致错误的路线:

@appHandler.route('/user/<int:userid>/', defaults = {'username' : None})
@appHandler.route('/user/<int:userid>/<username>/')

def profile(userid, username):
    user = User.query.filter_by(id = userid).first()

    if user.username != username:
        redirect(url_for(profile(userid, user.username)))

    return 'Finally!'

现在,当我输入

http://localhost:5000/user/14/sdfsdf/

http://localhost:5000/user/14/

我收到此错误:

werkzeug.routing.BuildError
BuildError: ('Finally!', {}, None)

Traceback (most recent call last):
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/kevin/Code/python/flask/terminus/app/routes.py", line 32, in profile
    redirect(url_for(profile(userid, user.username)))
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/helpers.py", line 312, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/flask/helpers.py", line 305, in url_for
    force_external=external)
  File "/home/kevin/Code/python/flask/terminus/venv/lib/python2.7/site-packages/werkzeug/routing.py", line 1620, in build
    raise BuildError(endpoint, values, method)
BuildError: ('Finally!', {}, None)

https://gist.github.com/de115414a70171f048fb

仅供参考,userid = 13username = sdfsdfuser.username = rohitrk

帮助会很棒。谢谢!

1 个答案:

答案 0 :(得分:1)

url_forstring作为其第一个参数(它是端点的名称,最常见但不总是函数的名称)和url参数或查询字符串值的关键字参数。尝试:

url_for('profile', userid=userid, username=user.username)

在你的情况下发生的事情是profile正在调用它的第二个实例(当你在url_for中调用它并返回'Finally'时。这意味着你是主叫:

url_for('Finally')

有效地产生BuildError