I can't find any information about generating a url using array querystrings like so: http://www.domain.com?page[limit]=20&page[offset]=0
I tried this:
url_for(endpoint, page={'limit': 0, 'offset': 0}, _external=True)
But it generated the following url:
http://www.domain.com?page={'limit': 0, 'offset': 0}
My current solution is as follows:
querystrings = []
querystrings.append('page[limit]=%d' % (limit))
querystrings.append('page[offset]=%d' % (offset))
url = '%s?%s' % (root_url, '&'.join(querystrings))
I really hope there is a better way!
Any help would be appreciated!
Edit
I ended up creating a wrapper which handles the dicts separately, based on my previous solution:
from flask import g, url_for as _url_for
def url_for(endpoint, **values):
# fix querystring dicts
querystring_dicts = []
for key, value in list(values.items()):
if isinstance(value, dict):
for _key, _value in list(value.items()):
querystring_dicts.append('%s[%s]=%s' % (key, _key, _value))
values.pop(key)
# create url
url = _url_for(endpoint, **values)
# append querystring dicts
if querystring_dicts:
seperator = '?'
if '?' in url:
seperator = '&'
url = '%s%s%s' % (url, seperator, '&'.join(querystring_dicts))
return url
I then call the wrapper like so:
url_for(endpoint, page={'limit': 20, 'offset': 0}, _external=True)
And it will return the following url: