Skip to content

Instantly share code, notes, and snippets.

@aliev
Created September 14, 2015 23:26
Show Gist options
  • Save aliev/101a3ccbfaea14949c52 to your computer and use it in GitHub Desktop.
Save aliev/101a3ccbfaea14949c52 to your computer and use it in GitHub Desktop.
from django import template
register = template.Library()
@register.simple_tag
def url_encode(request, **kwargs):
"""
For example, on the page, we have
1. filters,
2. pagination,
3. sorting.
we need to unite the url for each of these actions
through the ampersand, and they should not be repeated.
How it use:
For integer values
{% url_encode request url_key=url_value %}
For string values
{% url_encode request url_key='url_value' %}
Example for pagination:
{% url_encode request page=page %}
We can add more parameters separated by a space:
{% url_encode request order_by='price' ordering='asc' %}
"""
dict_ = request.GET.copy()
for key, value in kwargs.iteritems():
dict_[key] = value
return dict_.urlencode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment