Django template add GET parameter after current URL
Question:
Previously, I handled my translation with GET
parameter, by requesting the ?lang=<lang_code>
on URL.
<h6>{% trans "Translate" %}</h6>
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<span id="language-icons">
{% for language in languages %}
{% if language.code == 'id' %}
<a href="?lang=id" title="Indonesia">
<img height="20px" src="{% static 'icons/flags/id.svg' %}">
</a>
{% elif language.code == 'en' %}
<a href="?lang=en" title="English">
<img height="20px" src="{% static 'icons/flags/us.svg' %}">
</a>
{% endif %}
{% endfor %}
</span>
When I handle with {{ request.META.QUERY_STRING }}
,
and the previous URL already included lang=
param (like: /path/to/?page=2&lang=en
).
The next lang=
will attempts multiple times (like: /path/to/?page=2&lang=en&lang=id
).
I want to replace the old lang=en
param with new lang=id
. So, the url should be /path/to/?page=2&lang=id
.
Here is what I was tried;
{% with QUERY_STRING=request.META.QUERY_STRING %}
{% if QUERY_STRING %}
{% if 'lang' in QUERY_STRING %}
<a href="?{{ QUERY_STRING|replace_with:"lang=id" }}"> <!-- something like `replacer` -->
{% else %}
<a href="?{{ QUERY_STRING }}?lang=id %}">...</a>
{% endif %}
{% else %}
<a href="?lang=id">...</a>
{% endif %}
{% endwith %}
I just think, the above problem maybe nice when handle with templatetags, like:
@register.filter
def append_url_param(request, replacer='lang=id'):
params = request.META.QUERY_STRING # 'id=1&lang=en'
replacer_list = replacer.split('=')
if len(replacer_list) > 1:
key = replacer_list[0]
value = replacer_list[-1]
if key in params:
params = params.replace(????)
Answers:
Finally, I solved this problem with urllib.parse
library:
from urllib import parse
@register.filter
def append_url_param(url, rep='lang=id'):
"""
function to replace the query string of url.
:param `url` is string full url or GET query params.
:param `rep` is string replacer.
"""
rep_list = rep.split('=')
queries = url
if len(rep_list) > 1:
rep_key = rep_list[0]
rep_val = rep_list[1]
if 'http' in url:
queries = parse.urlsplit(url).query
dict_params = dict(parse.parse_qsl(queries))
dict_params.update({rep_key: rep_val})
queries_list = []
for (k, v) in dict_params.items():
queries_list.append('%s=%s' % (k, v))
base_url_list = url.split('?')
base_url = base_url_list[0]
if len(base_url_list) <= 1:
base_url = ''
queries_str = '&'.join(queries_list)
return '%s?%s' % (base_url, queries_str)
return url
and to use in the template;
<a href="{{ request.META.QUERY_STRING|append_url_param:'lang=id' }}">...</a>
<a href="{{ request.META.QUERY_STRING|append_url_param:'lang=en' }}">...</a>
The result example;
<a href="?page=1&q=abc&lang=id">...</a>
<a href="?id=3&lang=en">...</a>
Console example;
>>> url = 'http://127.0.0.1:8000/add-edit/?id=3&page=1&lang=en'
>>> append_url_param(url=url, rep='lang=id')
'http://127.0.0.1:8000/add-edit/?id=3&page=1&lang=id'
>>>
>>>
>>> url = 'id=3&page=1&lang=id'
>>> append_url_param(url=url, rep='lang=en')
'?id=3&page=1&lang=en'
>>>
Previously, I handled my translation with GET
parameter, by requesting the ?lang=<lang_code>
on URL.
<h6>{% trans "Translate" %}</h6>
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<span id="language-icons">
{% for language in languages %}
{% if language.code == 'id' %}
<a href="?lang=id" title="Indonesia">
<img height="20px" src="{% static 'icons/flags/id.svg' %}">
</a>
{% elif language.code == 'en' %}
<a href="?lang=en" title="English">
<img height="20px" src="{% static 'icons/flags/us.svg' %}">
</a>
{% endif %}
{% endfor %}
</span>
When I handle with {{ request.META.QUERY_STRING }}
,
and the previous URL already included lang=
param (like: /path/to/?page=2&lang=en
).
The next lang=
will attempts multiple times (like: /path/to/?page=2&lang=en&lang=id
).
I want to replace the old
lang=en
param with newlang=id
. So, the url should be/path/to/?page=2&lang=id
.
Here is what I was tried;
{% with QUERY_STRING=request.META.QUERY_STRING %}
{% if QUERY_STRING %}
{% if 'lang' in QUERY_STRING %}
<a href="?{{ QUERY_STRING|replace_with:"lang=id" }}"> <!-- something like `replacer` -->
{% else %}
<a href="?{{ QUERY_STRING }}?lang=id %}">...</a>
{% endif %}
{% else %}
<a href="?lang=id">...</a>
{% endif %}
{% endwith %}
I just think, the above problem maybe nice when handle with templatetags, like:
@register.filter
def append_url_param(request, replacer='lang=id'):
params = request.META.QUERY_STRING # 'id=1&lang=en'
replacer_list = replacer.split('=')
if len(replacer_list) > 1:
key = replacer_list[0]
value = replacer_list[-1]
if key in params:
params = params.replace(????)
Finally, I solved this problem with urllib.parse
library:
from urllib import parse
@register.filter
def append_url_param(url, rep='lang=id'):
"""
function to replace the query string of url.
:param `url` is string full url or GET query params.
:param `rep` is string replacer.
"""
rep_list = rep.split('=')
queries = url
if len(rep_list) > 1:
rep_key = rep_list[0]
rep_val = rep_list[1]
if 'http' in url:
queries = parse.urlsplit(url).query
dict_params = dict(parse.parse_qsl(queries))
dict_params.update({rep_key: rep_val})
queries_list = []
for (k, v) in dict_params.items():
queries_list.append('%s=%s' % (k, v))
base_url_list = url.split('?')
base_url = base_url_list[0]
if len(base_url_list) <= 1:
base_url = ''
queries_str = '&'.join(queries_list)
return '%s?%s' % (base_url, queries_str)
return url
and to use in the template;
<a href="{{ request.META.QUERY_STRING|append_url_param:'lang=id' }}">...</a>
<a href="{{ request.META.QUERY_STRING|append_url_param:'lang=en' }}">...</a>
The result example;
<a href="?page=1&q=abc&lang=id">...</a>
<a href="?id=3&lang=en">...</a>
Console example;
>>> url = 'http://127.0.0.1:8000/add-edit/?id=3&page=1&lang=en'
>>> append_url_param(url=url, rep='lang=id')
'http://127.0.0.1:8000/add-edit/?id=3&page=1&lang=id'
>>>
>>>
>>> url = 'id=3&page=1&lang=id'
>>> append_url_param(url=url, rep='lang=en')
'?id=3&page=1&lang=en'
>>>