两个使用Flask-Restful的变量Url [英] Two Variable Urls using Flask-Restful
问题描述
我正在写一个api,我想要一个网址看起来像这样:
'/ api / v1.0 / restaurant / Name& Address'
使用Flask-restful,我将url定义为
<$ p $ < code>'/ api / v1.0 / restaurant /< name>&< address>'
Werkzeug不喜欢这个,但是在werkzeug / routing.py中引发了一个BuildError
当我使用add_resource定义url时,
'/ api / v1.0 / restaurant /< name>'
和硬连线地址,一切正常。
如何定义网址取两个变量?
$ p
$ b $ $ p $ $ $ $ c> Traceback(最近调用最后一个):
文件/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask/app .py,第1475行,在full_dispatch_request
rv = self.dispatch_request()
文件/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask/ app.py,第1461行,在dispatch_request
返回self.view_functions [rule.endpoint](** req.view_args)
文件/home/ubuntu/.virtualenvs/data/local/lib/ python2.7 / site-packages / flask_restful / __init__.py,第397行,在包装中
resp = resource(* args,** kwargs)
文件/home/ubuntu/.virtualenvs/data /local/lib/python2.7/site-packages/flask/views.py,第84行,在
返回self.dispatch_request(* args,** kwargs)
文件/ home / ubuntu / .virtualenvs / data / local / lib / python2.7 / site-packages / flask_restful / __init__.py,第487行,在dispatch_request
resp = meth(* args,** kwargs)
/ home / subuntu / Hotsauce / api / app / views.py,第75行, Ubuntu的/ .virtualenvs /数据/ local / lib目录/ python2.7 / site-packages中/flask_restful/__init__.py,第533行,编组
返回OrderedDict(items)
文件/usr/lib/python2.7/collections.py,第52行,在__init__
self .__ update(* args,** kwds)
文件/home/ubuntu/.virtualenvs/data/lib/python2.7/_abcoll.py,第547行,更新
,其他值如下:
在< genexpr>文件中的/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/__init__.py,第532行。
for k,v in fields.items())
文件/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/fields.py,第232行,输出
o = urlparse(url_for(self.endpoint,_external = self.absolute,** data))
文件/home/ubuntu/.virtualenvs/data/local/lib/python2 .7 / site-packages / flask / helpers.py,第312行,在url_for
中返回appctx.app.handle_url_build_error(error,endpoint,values)
文件/home/ubuntu/.virtualenvs/ data / local / lib / python2.7 / site-packages / flask / app.py,第1641行,在handle_url_build_error
reraise(exc_type,exc_value,tb)
文件/ home / ubuntu / virtualenvs / data / local / lib / python2.7 / site-packages / flask / helpers.py,第305行,在url_for
force_external = external)
文件/home/ubuntu/.virtualenvs/ data / local / lib / python2.7 / site-packages / werkzeug / routing.py,第1620行,在build
引发BuildError(endpoint,values,method)
BuildError :('restaurant' {u'city_id':2468,u'score :Decimal('0E-10'),'id':37247,u'nbhd_id':6596,u'address_region':u'NY',u'phone_number':u'(718)858-6700','_sa_instance_state ':< sqlalchemy.orm.state.InstanceState object at 0x26f33d0> ;, u'complete':False,u'name':u'Asya',u'address_locality':u'New York',u'address_updated':True ,u'street_address':u'46 Henry St'},None)
这里是相关的生成错误的代码:
resto_fields = {
'id':fields.Integer,
'名字':fields.String,
'street_address':fields.String,
'address_locality':fields.String,
'address_region':fields.String,
'score' :fields.Float,
'phone_number':fields.String,
'uri':fields.Url('restaurant')
}
def get(self ,姓名,地址):
restaurant = session.query(Restaurant).filter_by(name = name)。 filter_by(address = address)
resto = {'restaurant':marshal(restaurant,resto_fields)}
return resto
这与&
&符号无关也没有使用多个URL参数。
您只能使用 resto_fields
输出字段映射中的条目您的端点;在 resto_fields
映射中,您没有地址
条目,但您的 restaurant
端点需要它建立URL。
添加一个地址
字段添加到您的输出字段,或使用路径中现有的字段之一。
This seems like something that would come up a lot, but I can't find any documentation on it.
I'm writing an api and I want urls to look like this:
'/api/v1.0/restaurant/Name&Address'
Using Flask-restful, I've defined the url as
'/api/v1.0/restaurant/<name>&<address>'
Werkzeug doesn't like this however and raises a BuildError in werkzeug/routing.py
When I define the url, with add_resource, as
'/api/v1.0/restaurant/<name>'
and hard-wire the address, everything works fine.
How do I define the urls to take two variables?
Edit
Traceback (most recent call last):
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 397, in wrapper
resp = resource(*args, **kwargs)
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 487, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/ubuntu/Hotsauce/api/app/views.py", line 75, in get
resto = {'restaurant': marshal(restaurant, resto_fields)}
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 533, in marshal
return OrderedDict(items)
File "/usr/lib/python2.7/collections.py", line 52, in __init__
self.__update(*args, **kwds)
File "/home/ubuntu/.virtualenvs/data/lib/python2.7/_abcoll.py", line 547, in update
for key, value in other:
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 532, in <genexpr>
for k, v in fields.items())
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/fields.py", line 232, in output
o = urlparse(url_for(self.endpoint, _external = self.absolute, **data))
File "/home/ubuntu/.virtualenvs/data/local/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/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask/helpers.py", line 305, in url_for
force_external=external)
File "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/werkzeug/routing.py", line 1620, in build
raise BuildError(endpoint, values, method)
BuildError: ('restaurant', {u'city_id': 2468, u’score’: Decimal('0E-10'), 'id': 37247, u'nbhd_id': 6596, u'address_region': u'NY', u'phone_number': u'(718) 858-6700', '_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x26f33d0>, u'complete': False, u'name': u'Asya', u'address_locality': u'New York', u'address_updated': True, u'street_address': u'46 Henry St'}, None)
And here is the relevant code that generates the error:
resto_fields = {
'id': fields.Integer,
'name': fields.String,
'street_address': fields.String,
'address_locality': fields.String,
'address_region': fields.String,
‘score’: fields.Float,
'phone_number': fields.String,
'uri': fields.Url('restaurant')
}
def get(self, name, address):
restaurant = session.query(Restaurant).filter_by(name=name).filter_by(address=address)
resto = {'restaurant': marshal(restaurant, resto_fields)}
return resto
This has nothing to do with the &
ampersand nor with with using more than one URL parameter.
You can only use entries from the resto_fields
output fields mapping in your endpoint; you don't have an address
entry in your resto_fields
mapping, but your restaurant
endpoint requires it to build the URL.
Add an address
field to your output fields, or use one of the existing fields in the route.
这篇关于两个使用Flask-Restful的变量Url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!