4

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.

3
  • 1
    Maybe pass something like **request.args (untested), that is passing request.args multidict as keywords arguments. Commented Apr 25, 2016 at 16:32
  • Thanks @Jérôme!! <a href="{{url_for('test', _external=True,**request.args)}}"> Test Args </a> Seems to be working! Commented Apr 25, 2016 at 16:49
  • Great. I detailed the answer with better explanations. Commented Apr 25, 2016 at 19:49

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.