Skip to content

url_for appends ? if called with empty query argument #1992

@lucaswerkmeister

Description

@lucaswerkmeister

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.)

@app.route('/')
def index():
    return flask.url_for('index', extra_param=[])

Full example: https://github.com/lucaswerkmeister/repro-flask-url_for-empty

Actual Behavior

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

Environment

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

Workaround

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions