Create dynamic arguments for url_for in Flask

Any arguments that don’t match route parameters will be added as the query string. Assuming extra_args is a dict, just unpack it.

extra_args = {'hello': 'world'}
url_for('doit', oid=oid, **extra_args)
# /doit/123?hello=world
url_for('doit', oid=oid, hello='davidism')
# /doit/123?hello=davidism

Then access them in the view with request.args:

@app.route('/doit/<int:oid>')
def doit(oid)
    hello = request.args.get('hello')
    ...

Leave a Comment