Flask-Restful Url字段尝试使用不正确的参数构建url

时间:2017-02-21 11:57:35

标签: python flask flask-restful

我想在其序列化中添加资源的网址。但是,使用fields.Url引发werkzeug.routing.BuildError: Could not build url for endpoint 'url.user_endpoint' with values ['attribute_values']. Did you forget to specify values ['github_uuid']?如何构建指向用户端点的链接?

from flask_restful import Resource, marshal_with
from flask_restful import fields
from app.api import api
from app.api.autherization.service.userservice import UserService

user_fields = {
    'uri': fields.Url('.user_endpoint'),
}

user_manager = UserService()

class UserResource(Resource):
    @marshal_with(user_fields)
    def get(self, github_uuid):
        return user_manager.get_user(github_uuid)

api.add_resource(UserResource, '/users/<int:github_uuid>', endpoint='user_endpoint')
Traceback (most recent call last):
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_cors\extension.py", line 188, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
    return original_handler(e)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\_compat.py", line 32, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_cors\extension.py", line 188, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
    return original_handler(e)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\_compat.py", line 32, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 477, in wrapper
    resp = resource(*args, **kwargs)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 587, in dispatch_request
    resp = meth(*args, **kwargs)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 684, in wrapper
    return marshal(resp, self.fields, self.envelope)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 640, in marshal
    return OrderedDict([(envelope, OrderedDict(items))]) if envelope else OrderedDict(items)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 639, in <genexpr>
    for k, v in fields.items())
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\fields.py", line 155, in output
    return marshal(value, self.nested)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 635, in marshal
    if envelope else [marshal(d, fields) for d in data])
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 635, in <listcomp>
    if envelope else [marshal(d, fields) for d in data])
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 640, in marshal
    return OrderedDict([(envelope, OrderedDict(items))]) if envelope else OrderedDict(items)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\__init__.py", line 639, in <genexpr>
    for k, v in fields.items())
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask_restful\fields.py", line 307, in output
    o = urlparse(url_for(endpoint, _external=self.absolute, **data))
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\helpers.py", line 332, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\app.py", line 1811, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\sangam\Anaconda3\lib\site-packages\flask\helpers.py", line 322, in url_for
    force_external=external)
  File "C:\Users\sangam\Anaconda3\lib\site-packages\werkzeug\routing.py", line 1758, in build
    raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'url.user_endpoint' with values ['attribute_values']. Did you forget to specify values ['github_uuid']?

0 个答案:

没有答案