Append paramaters to a GET querystring (template tag)

 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
from django import template

register = template.Library()

"""
Decorator to facilitate template tag creation
"""
def easy_tag(func):
    """deal with the repetitive parts of parsing template tags"""
    def inner(parser, token):
        #print token
        try:
            return func(*token.split_contents())
        except TypeError:
            raise template.TemplateSyntaxError('Bad arguments for tag "%s"' % token.split_contents()[0])
    inner.__name__ = func.__name__
    inner.__doc__ = inner.__doc__
    return inner



class AppendGetNode(template.Node):
    def __init__(self, dict):
        self.dict_pairs = {}
        for pair in dict.split(','):
            pair = pair.split('=')
            self.dict_pairs[pair[0]] = template.Variable(pair[1])
            
    def render(self, context):
        get = context['request'].GET.copy()

        for key in self.dict_pairs:
            get[key] = self.dict_pairs[key].resolve(context)
        
        path = context['request'].META['PATH_INFO']
        
        #print "&".join(["%s=%s" % (key, value) for (key, value) in get.items() if value])
        
        if len(get):
            path += "?%s" % "&".join(["%s=%s" % (key, value) for (key, value) in get.items() if value])
        
        
        return path

@register.tag()
@easy_tag
def append_to_get(_tag_name, dict):
    return AppendGetNode(dict)

More like this

  1. Digg-like pagination by SmileyChris 4 years, 11 months ago
  2. A tip for preserving GET arguments with pagination by achimnol 4 years, 10 months ago
  3. better paginator template tag by amitu 5 years, 6 months ago
  4. Pagination/Filtering Alphabetically by zain 5 years, 1 month ago
  5. Page numbers with ... like in Digg by Ciantic 5 years ago

Comments

gregb (on July 13, 2009):

Oops, thanks for pointing that out.

#

udfalkso (on April 29, 2010):

Thanks! I was just about to do something similar.

#

quietbob (on July 19, 2010):

Thanks, very handy. Just a note, I had to change line 40 to:

    path += "?%s" % get.urlencode()

to get things to work correctly in the presence of QueryDicts with multiple values for the same key.

#

quietbob (on July 19, 2010):

Thanks, very handy. Just a note, I had to change line 40 to:

    path += "?%s" % get.urlencode()

to get things to work correctly in the presence of QueryDicts with multiple values for the same key.

#

kneufeld (on April 29, 2011):

You need to have the request context processor.

TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.request',
...
)

#

peroksid (on December 16, 2012):

class AppendGetNode(template.Node): def init(self, d): self.dict_pairs = [] for pair in d.split(','): k, v = pair.split('=') self.dict_pairs.append((k, template.Variable(v)))

def render(self, context):
    request = context['request']
    get = request.GET.copy()
    for k, v in self.dict_pairs:
        get[k] = v.resolve(context)
    return "%s?%s" % (request.path, get.urlencode())

#

(Forgotten your password?)