Changing request parameters in the current GET request for a new URL

I am accessing a page with the outline /mypage?a=1&b=1&c=1 . I want to create a link to a similar url with some parameters changed: /mypage?a=1&b=2&c=1 , b changed from 1 to 2. I know how to get the current arguments from request.args , but the structure is unchanged, so I don’t know t know how to edit them. How to create a new link in a Jinja template with a modified request?

+7
python url flask jinja2
source share
1 answer

Write a function that modifies the current url query string and displays a new url. Add a function to global templates using the Flask app template_global application so that it can be used in Jinja templates.

 from flask import request from werkzeug.urls import url_encode @app.template_global() def modify_query(**new_values): args = request.args.copy() for key, value in new_values.items(): args[key] = value return '{}?{}'.format(request.path, url_encode(args)) 
 <a href="{{ modify_query(b=2) }}">Link with updated "b"</a> 
+11
source share