Reputation: 43
I'm curious if anyone has come up with a good solution for passing unknown URL arguments in flask. I think there has to be an easy solution that I just am totally blanking on. Any help is appreciated.
Let's say I have www.example.com?foo=baz&bar=buzz
@app.route('/')
def index():
foo = request.args.get('foo',None)
bar = request.args.get('bar',None)
return redirect(url_for('args', foo=foo, bar=bar, _external=True))
What I can't seem to figure out is a good way to pass along any unknown arguments. If I have www.example.com?foo=baz&bar=buzz&bing=bang
or www.example.com?this=that
coming to the same route I need to get any/and
all arguments to pass along to the next view.
Upvotes: 4
Views: 899
Reputation: 14664
request.args is a MultiDict.
A MultiDict is a dictionary subclass customized to deal with multiple values for the same key which is for example used by the parsing functions in the wrappers. This is necessary because some HTML form elements pass multiple values for the same key.
MultiDict implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key.
Assuming you don't mind the multiple values issue, you may use it as any dictionary and pass it as keyword arguments using the double *
notation:
url_for('test', _external=True, **request.args)
Upvotes: 3