Login

Add GET parameters from current request

Author:
naktinis
Posted:
May 4, 2011
Language:
Python
Version:
Not specified
Tags:
get request parameters add
Score:
2 (after 2 ratings)

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

 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

Comments

jsz (on September 15, 2011):
<p>Thanks for the snippet. But I got a problem here. The resulting page seems to get a validation error from the w3c validator.</p>
<p>general entity "page" not defined and no default entity reference to entity "page" for which no system identifier could be generated</p>
<p>(page is the parameter I passed to the tag).</p> <p>So there seems to be some escaping pitfalls.</p>

#

nailgun (on March 19, 2012):
<p>I would avoid empty params:</p> <p>class AddGetParameter(Node):</p> <pre>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() </pre>

#

Please login first before commenting.