Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url_for appends ? if called with empty query argument #1992

lucaswerkmeister opened this issue Jan 6, 2021 · 2 comments · Fixed by #2004

url_for appends ? if called with empty query argument #1992

lucaswerkmeister opened this issue Jan 6, 2021 · 2 comments · Fixed by #2004


Copy link

Expected Behavior

If url_for is called with a query argument that is an empty list (and no other query arguments), the URL should have no query part at all, including no trailing question mark. (When called with a nonempty list, it adds several query arguments with the same name, which is what I expect; when called with None, it omits the question mark already.)

def index():
    return flask.url_for('index', extra_param=[])

Full example:

Actual Behavior

The URL has a question mark at the end (in the above example: /?).


  • Python version: 3.9.1
  • Flask version: 1.1.2
  • Werkzeug version: 1.0.1


Replace the empty list with None by appending or None to the argument. (This also works in Jinja2 templates.)

Copy link

davidism commented Jan 6, 2021

The version with the question mark is valid. I can't remember the exact reason, but this has been discussed before. If you are in a situation where you'll be sending potentially empty arguments and don't want it, you can write a wrapper to strip it off.

@davidism davidism transferred this issue from pallets/flask Jan 6, 2021
Copy link

Went back to 0.14 and this didn't happen there. git bisect points to #1281, which introduced the new code generator for URL building.

@davidism davidism added the bug label Jan 15, 2021
@davidism davidism added this to the 2.0.0 milestone Jan 15, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

Successfully merging a pull request may close this issue.

2 participants