1 from django
import template
3 register
= template
.Library()
5 class BuildQueryStringNode(template
.Node
):
6 def __init__(self
, sortfield
):
7 self
.sortfield
= sortfield
8 def render(self
, context
):
9 qs
= context
['querystring'].copy()
10 if qs
.has_key('sort') and qs
['sort'] == self
.sortfield
:
11 qs
['sort'] = '-' + self
.sortfield
13 qs
['sort'] = self
.sortfield
14 return '?' + qs
.urlencode()
16 @register.tag(name
='buildsortqs')
17 def do_buildsortqs(parser
, token
):
19 tagname
, sortfield
= token
.split_contents()
21 raise template
.TemplateSyntaxError
, "%r tag requires a single argument" % tagname
22 if not (sortfield
[0] == sortfield
[-1] and sortfield
[0] in ('"', "'")):
23 raise template
.TemplateSyntaxError
, "%r tag's argument should be in quotes" % tagname
24 return BuildQueryStringNode(sortfield
[1:-1])
26 @register.filter(name
='space2br')
28 return value
.replace(' ', '<br />')