Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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 如何在带有端点的restful中为field.url指定值_Python_Flask_Flask Restful - Fatal编程技术网

Python 如何在带有端点的restful中为field.url指定值

Python 如何在带有端点的restful中为field.url指定值,python,flask,flask-restful,Python,Flask,Flask Restful,如何在flask restful中自定义field.url user_fields = { ... 'test': fields.Url('userep', absolute=True) .... } api.add_resource(User, '/user', '/user/<int:userid>', endpoint='userep') 当我提交http://127.0.0.1:5000/user/1 抛出如下错误: werkzeug.routin

如何在flask restful中自定义field.url

user_fields = {
    ...
    'test': fields.Url('userep', absolute=True)
    ....
}

api.add_resource(User, '/user', '/user/<int:userid>', endpoint='userep')
当我提交
http://127.0.0.1:5000/user/1
抛出如下错误:

werkzeug.routing.BuildError

BuildError:无法为终结点“/Users/{id}/Friends”生成url 具有值[''sa_instance_state',email',id',昵称', “密码”、“注册日期”、“状态”]。你是说“版本”吗

有什么建议吗?thx

进一步来说,如果我更改资源

api.add_资源(User,'/User/',endpoint='userep')

错误消息抛出

werkzeug.routing.BuildError

BuildError:无法为具有值的终结点“userep”生成url [“sa_实例状态”、“电子邮件”、“id”、“昵称”、“密码”, '注册日期','状态']。您是否忘记指定值['userid']

在正式文档字段中.url

class Url(Raw):
"""
A string representation of a Url
:param endpoint: Endpoint name. If endpoint is ``None``,
    ``request.endpoint`` is used instead
:type endpoint: str
:param absolute: If ``True``, ensures that the generated urls will have the
    hostname included
:type absolute: bool
:param scheme: URL scheme specifier (e.g. ``http``, ``https``)
:type scheme: str
"""
def __init__(self, endpoint=None, absolute=False, scheme=None):
    super(Url, self).__init__()
    self.endpoint = endpoint
    self.absolute = absolute
    self.scheme = scheme

def output(self, key, obj):
    try:
        data = to_marshallable_type(obj)
        endpoint = self.endpoint if self.endpoint is not None else request.endpoint
        o = urlparse(url_for(endpoint, _external=self.absolute, **data))
        if self.absolute:
            scheme = self.scheme if self.scheme is not None else o.scheme
            return urlunparse((scheme, o.netloc, o.path, "", "", ""))
        return urlunparse(("", "", o.path, "", "", ""))
    except TypeError as te:
        raise MarshallingException(te)

自己回答:这不是解决问题的办法

根据项目问题:和

代码如下:

user_fields = {
    'id': fields.Integer,
    'friends': fields.Url('/Users/{id}/Friends'),
from flask import url_for

class ProjectsView(object):
    def __init__(self, projectid):
        self.projectid = projectid
        ...

    def serialize(self):
        return {
            ...
            'tasks_url':url_for('.getListByProjectID', _external=True, projectid=self.projectid),
        }

class Projects(Resource):
    def get(self, userid):
        project_obj_list = []
        ...
        v = ProjectsView(project.id)
        project_obj_list.append(v)

    return jsonify(result=[e.serialize() for e in project_obj_list])
{
  "result": [
    {
      ... 
      "tasks_url":"http://127.0.0.1:5000/api/v1.0/1/GetListByProjectID"
    }
  ]
}
回答是这样的:

user_fields = {
    'id': fields.Integer,
    'friends': fields.Url('/Users/{id}/Friends'),
from flask import url_for

class ProjectsView(object):
    def __init__(self, projectid):
        self.projectid = projectid
        ...

    def serialize(self):
        return {
            ...
            'tasks_url':url_for('.getListByProjectID', _external=True, projectid=self.projectid),
        }

class Projects(Resource):
    def get(self, userid):
        project_obj_list = []
        ...
        v = ProjectsView(project.id)
        project_obj_list.append(v)

    return jsonify(result=[e.serialize() for e in project_obj_list])
{
  "result": [
    {
      ... 
      "tasks_url":"http://127.0.0.1:5000/api/v1.0/1/GetListByProjectID"
    }
  ]
}