16๐
There is no builtin support, but you can add one yourself. You can for example define the following template tag. We can for example construct files in boldface:
app/
templatetags/
__init__.py
urlparams.py
Where in urlparams.py
, we define:
from django import template
from urllib.parse import urlencode
register = template.Library()
@register.simple_tag
def urlparams(*_, **kwargs):
safe_args = {k: v for k, v in kwargs.items() if v is not None}
if safe_args:
return '?{}'.format(urlencode(safe_args))
return ''
In the template, we can then load the template tag and then use it like with:
{% load urlparams %}
<a href="{% url 'videos:index'}{% urlparams page='1' tag='sometag' %}">Next</a>
Note that strictly speaking, the URL parameters can contain the same key multiple times. This is here not possible. So we can not generate all possible URL parameters, but this is usually quite rare, and in my opinion not a good idea in the first place.
2๐
you can use default template filter
and update your example
<a class="link-button" href="{% url 'videos:index' %}?tag={{ tag|default:'' }}&page={{ next|defaul:'' }}">Next</a>
output for empty tag and page is:
http://127.0.0.1:8000/videos/?tag=&page=
but if you want to dont print None Tag in url you must your own template tag or filter. simply you can write this template filter
@register.filter
def print_query_param(value, key)
if value and key:
return "%s=%s&" % (key, value)
and you can use it as below
<a class="link-button" href="{% url 'videos:index' %}?{{ tag|print_query_param:'tag' }}{{ next|print_query_param:'page' }}">Next</a>
1๐
Template tag urlencode
Use the template tag urlencode
:
<a href="example?tag={{ tag|urlencode }}&page={{ next|urlencode }}">example</a>
Small note:
Note that there is no need to pass the param names themselves through urlencode
, since in this case they are literals. If the param names were not literals, you would need to, like this:
<a href="example?{{ name1|urlencode }}={{ value1|urlencode }}&{{ name2|urlencode }}={{ value2|urlencode }}">example</a>
- Getting error with is_popup variable in django 1.9
- Django-rest-framework: add additional permission in ViewSet update method
- ImportError: cannot import name generic
- Docker, web app static files. Best practices?