- Author:
- naktinis
- Posted:
- May 4, 2011
- Language:
- Python
- Version:
- Not specified
- Score:
- 2 (after 2 ratings)
The tag generates a parameter string in form '?param1=val1¶m2=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¶m2=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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Thanks for the snippet. But I got a problem here. The resulting page seems to get a validation error from the w3c validator.
(page is the parameter I passed to the tag).
So there seems to be some escaping pitfalls.
#
I would avoid empty params:
class AddGetParameter(Node):
#
Please login first before commenting.