Add GET parameters from current request

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.template import Library, Node, resolve_variable

register = Library()

"""
The tag generates a parameter string in form '?param1=val1&param2=val2'.
The parameter list is generated by taking all parameters from current
request.GET and optionally overriding them by providing parameters to the tag.

This is a cleaned up version of http://djangosnippets.org/snippets/2105/. It
solves a couple of issues, namely:
 * parameters are optional
 * parameters can have values from request, e.g. request.GET.foo
 * native parsing methods are used for better compatibility and readability
 * shorter tag name

Usage: place this code in your appdir/templatetags/add_get_parameter.py
In template:
{% load add_get_parameter %}
<a href="{% add_get param1='const' param2=variable_in_context %}">
Link with modified params
</a>

It's required that you have 'django.core.context_processors.request' in
TEMPLATE_CONTEXT_PROCESSORS

Original version's URL: http://django.mar.lt/2010/07/add-get-parameter-tag.html
"""

class AddGetParameter(Node):
    def __init__(self, values):
        self.values = values
        
    def render(self, context):
        req = resolve_variable('request', context)
        params = req.GET.copy()
        for key, value in self.values.items():
            params[key] = value.resolve(context)
        return '?%s' %  params.urlencode()


@register.tag
def add_get(parser, token):
    pairs = token.split_contents()[1:]
    values = {}
    for pair in pairs:
        s = pair.split('=', 1)
        values[s[0]] = parser.compile_filter(s[1])
    return AddGetParameter(values)

More like this

  1. Add GET parameter tag by marltu 2 years, 10 months ago
  2. Querystring Builder - create urls with GET params by jibberia 2 years, 3 months ago
  3. Add site info to request context by bthomas 4 years, 6 months ago
  4. Paginator TemplateTag by trbs 5 years, 1 month ago
  5. Url persistance of GET variables by alex_ndc 4 years, 6 months ago

Comments

jsz (on September 15, 2011):

Thanks for the snippet. But I got a problem here. The resulting page seems to get a validation error from the w3c validator.

general entity "page" not defined and no default entity reference to entity "page" for which no system identifier could be generated

(page is the parameter I passed to the tag).

So there seems to be some escaping pitfalls.

#

nailgun (on March 19, 2012):

I would avoid empty params:

class AddGetParameter(Node):

def __init__(self, values):
    self.values = values

def render(self, context):
    req = resolve_variable('request', context)
    params = req.GET.copy()
    for key, value in self.values.items():
        resolved = value.resolve(context)
        if resolved:
            params[key] = value.resolve(context)
    return '?%s' %  params.urlencode()

#

akaihola (on October 19, 2012):

Here's a list of other implementations I could find for a Django template tag which modifies the current HTTP GET query string.

On djangosnippets.org:

On Stack Overflow:

On PyPI:

On GitHub:

#

(Forgotten your password?)