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

Open
lucaswerkmeister opened this issue Jan 6, 2021 · 1 comment
Open

url_for appends ? if called with empty query argument #1992

lucaswerkmeister opened this issue Jan 6, 2021 · 1 comment

Comments

@lucaswerkmeister
Copy link

@lucaswerkmeister lucaswerkmeister commented Jan 6, 2021

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

@davidism
Copy link
Member

@davidism 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.