python - Two Variable Urls using Flask-Restful -
this seems come lot, can't find documentation on it.
i'm writing api , want urls this:
'/api/v1.0/restaurant/name&address'
using flask-restful, i've defined url as
'/api/v1.0/restaurant/<name>&<address>'
werkzeug doesn't , raises builderror in werkzeug/routing.py
when define url, add_resource,
'/api/v1.0/restaurant/<name>'
and hard-wire address, works fine.
how define urls take 2 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 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 key, value in other: file "/home/ubuntu/.virtualenvs/data/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 532, in <genexpr> 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 @ 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 relevant code generates 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 &
ampersand nor with using more 1 url parameter.
you can use entries resto_fields
output fields mapping in endpoint; don't have address
entry in resto_fields
mapping, restaurant
endpoint requires build url.
add address
field output fields, or use 1 of existing fields in route.
Comments
Post a Comment