-
Howdy! Is there a built-in way to build an href to a new URL that maintains the existing query params while updating one or more of them? Or would I need to build that myself out of the existing For example, I have a URL Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@adam820 Is this what you are looking to do: import justpy as jp
def link_test(request):
wp = jp.WebPage()
foo = request.query_params.get('foo', 'No foo' )
foo2 = request.query_params.get('foo2', 'No foo2' )
jp.Div(text=f'foo: {foo}, foo2: {foo2}', classes="m-4 text-xl", a=wp)
jp.A(text='New link', href='http://localhost:8000/?foo=bar&foo2=buz', a=wp,
classes='m-4 p-2 text-xl text-white bg-blue-500 hover:bg-blue-700')
return wp
jp.justpy(link_test) If not, please let me know |
Beta Was this translation helpful? Give feedback.
-
Sort of, but not quite. I figured it could be done manually, but I was checking to see if there was something that would auto-build the href with the query parameters already intact (by passing them in), to avoid having to manually hardcode addresses, etc. I ended up making my own quick and dirty params builder function that updates an existing parameter, or otherwise accepts new parameters passed in as kwargs. from urllib.parse import urlencode
def build_url_params(req_obj, **kwargs):
# Copy the original query parameters over to a new mutable dict
qparams = dict()
for k, v in req_obj.query_params.items():
qparams[k] = v
try:
# Build the query params
for k, v in kwargs.items():
if k in qparams.keys():
qparams[k] = v
else:
qparams[k] = v
except (ValueError, KeyError):
return "?"
return f"?{urlencode(qparams)}" |
Beta Was this translation helpful? Give feedback.
@adam820 Is this what you are looking to do:
If not, please let me know