Skip to content

Instantly share code, notes, and snippets.

@benbacardi
Last active September 1, 2025 00:54
Show Gist options
  • Save benbacardi/d6cd0fb8c85e1547c3c60f95f5b2d5e1 to your computer and use it in GitHub Desktop.
Save benbacardi/d6cd0fb8c85e1547c3c60f95f5b2d5e1 to your computer and use it in GitHub Desktop.

Revisions

  1. benbacardi revised this gist Jun 29, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion templatetags.py
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,6 @@ def query_transform(context, **kwargs):
    A RequestContext is required for access to the current querystring.
    '''
    query = context['request'].GET.copy()
    for k, v in kwargs.iteritems():
    for k, v in kwargs.items():
    query[k] = v
    return query.urlencode()
  2. benbacardi revised this gist Sep 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion templatetags.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ def query_transform(context, **kwargs):
    updating the params with the key/value pairs passed to the tag.
    E.g: given the querystring ?foo=1&bar=2
    {% query_transform bar=3 %} outputs ?foo=1&bar=4
    {% query_transform bar=3 %} outputs ?foo=1&bar=3
    {% query_transform foo='baz' %} outputs ?foo=baz&bar=2
    {% query_transform foo='one' bar='two' baz=99 %} outputs ?foo=one&bar=two&baz=99
  3. benbacardi revised this gist Aug 24, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions templatetags.py
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,7 @@ def query_transform(context, **kwargs):
    E.g: given the querystring ?foo=1&bar=2
    {% query_transform bar=3 %} outputs ?foo=1&bar=4
    {% query_transform foo='baz' %} outputs ?foo=baz&bar=2
    {% query_transform foo='one' bar='two' baz=99 %} outputs ?foo=one&bar=two&baz=99
    A RequestContext is required for access to the current querystring.
    '''
  4. benbacardi created this gist Aug 24, 2016.
    20 changes: 20 additions & 0 deletions templatetags.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    from django import template

    register = template.Library()

    @register.simple_tag(takes_context=True)
    def query_transform(context, **kwargs):
    '''
    Returns the URL-encoded querystring for the current page,
    updating the params with the key/value pairs passed to the tag.
    E.g: given the querystring ?foo=1&bar=2
    {% query_transform bar=3 %} outputs ?foo=1&bar=4
    {% query_transform foo='baz' %} outputs ?foo=baz&bar=2
    A RequestContext is required for access to the current querystring.
    '''
    query = context['request'].GET.copy()
    for k, v in kwargs.iteritems():
    query[k] = v
    return query.urlencode()