python - Flask: URLs w/ Variable parameters -
i have url string want build in following manner:
http://something.com/mainsite/key1/key2/key3/keyn
how generate in url mapping n variable number?
how keys?
thanks
there 2 ways this:
simply use
path
route converter:@app.route("/mainsite/<path:varargs>") def api(varargs=none): # mainsite/key1/key2/key3/keyn # `varargs` string contain above varargs = varargs.split("/") # , list of strings
register own custom route converter (see werkzeug's documentation full details):
from werkzeug.routing import baseconverter, validationerror class pathvarargsconverter(baseconverter): """convert remaining path segments list""" def __init__(self, url_map): super(pathvarargsconverter, self).__init__(url_map) self.regex = "(?:.*)" def to_python(self, value): return value.split(u"/") def to_url(self, value): return u"/".join(value) app.url_map.converters['varargs'] = pathvarargsconverter
which can use this:
@app.route("/mainsite/<varargs:args>") def api(args): # args here list of path segments
Comments
Post a Comment