-
-
Notifications
You must be signed in to change notification settings - Fork 953
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
add support to request.url_for for query parameters #560
Comments
Totally agree with you, it would be very convenient. I propose to add all unmatched path params to query string. |
I think what we'll probably do here is have url = request.url_for('users', username="tom").with_query_params(...) |
Sounds great, but would there be a less wordy alternative? |
Perhaps just this...
Or an explicitly shorthand notation...
|
|
We probably want to differentiate it more clearly from the properties on the URL class. ( I think we probably want to end up with...
|
I see you're taking some care to think this through, so I'll be closing my open PR on this soon (been open for more than a month now) - the approach I took in the PR was more akin to the existing implementation. But I guess you want to refactor in order to provide a more proper support for the URL's query. And I'm defintely 👍 on that |
are there any updates? |
I implemented a naive workaround to this: def url_for_query(request: Request, name: str, **params: str) -> str:
url = request.url_for(name)
parsed = list(urllib.parse.urlparse(url))
parsed[4] = urllib.parse.urlencode(params)
return urllib.parse.urlparse(parsed)
templates = Jinja2Templates(directory='templates')
templates.env.globals['url_for_query'] = url_for_query Then, in template
This solution doesn't work for routes that accept a parameter. |
@natanlao
|
@gopalsingh462 Are you referring to the Flask documentation? |
I like this idea but I'm concerned that this will change the type signature of Like @cjw296 I'm coming to this issue for pagination purposes. I want to take the request's URL and replace some from urllib.parse import urlsplit, parse_qs, urlencode
def replace_query_params(url: str, query: dict) -> str:
_url = urlsplit(url)
_query = parse_qs(_url.query)
_query.update(query)
querystr = urlencode(_query, doseq=True)
return _url._replace(query=querystr).geturl() e.g., >>> replace_query_params('https://example.com/foo?filter=10&offset=0&limit=50', {'offset': 50})
'https://example.com/foo?filter=10&offset=50&limit=50' |
Do we have this feature yet? |
Can we close this @aminalaee ? |
Closing this, since we implemented #1385. Let me know if that's not enough, and we can reopen. |
It is enough. Thank you |
I totally misunderstood
url_for
's API, I was expectingurl_for('some_route', foo='bar')
to return'http://testserver/my_route/?foo=bar'
.I understand why that is now, but what is the best way to generate urls like this?
(for pagination in a rest list api in this case).
Could
url_for
grow support for query parameters in addition to path parameters?The text was updated successfully, but these errors were encountered: