Login

Template tag which gets specific GET variables from the current request

Author:
aruseni.magiku
Posted:
March 21, 2010
Language:
Python
Version:
1.1
Score:
2 (after 2 ratings)

This template tag attempts to get specific GET variables from request.GET. If such variables exist, it shows them as a query string (with optional "include_ampersand" mode when it puts an "&" at the end if there is a result string, or a "?" if there's none: it is used when you need to add a new variable to the query string) or as hidden input fields ("html_form" mode).

 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
50
# Usage:
#
# {% query_string request variables mode %}
#
# request:
# HttpRequest object
#
# variables:
# string of GET variables separed by spaces
#
# mode:
# 1. if mode is set to include_ampersand, and the result string is not empty,
#    the result string gets an ampersand at the end
# 2. if mode is set to include_ampersand, but there are no variables
#    in query string, a question mark goes to the result string
# 3. if mode is set to html_form, the result string turns to a set of
#    html hidden input fields.
# 4. or leave mode empty for the default behavior
#
# Example:
# {% query_string request "size colour" "include_ampersand" %}

from django.utils.safestring import mark_safe
from django.utils.html import escape
from django import template

register = template.Library()

def query_string(request, variables, mode):
    variable_list = variables.split(' ')
    query_string_dict = {}
    for variable in variable_list:
        value = request.GET.get(variable, '')
        if value:
            query_string_dict[variable] = escape(value)
    if query_string_dict:
        if mode == "html_form":
            query_string = ' '.join([u'<input type="hidden" name="%s" value="%s">' % (k, v) for k, v in query_string_dict.items()])
        else:
            query_string = '?' + '&amp;'.join([u'%s=%s' % (k, v) for k, v in query_string_dict.items()]).replace(' ', '%20')
            if mode == "include_ampersand":
                query_string += '&amp;'
    else:
        if mode == "include_ampersand":
            query_string = '?'
        else:
            query_string = ''
    return mark_safe(query_string)

register.simple_tag(query_string)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 3 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

aruseni.magiku (on March 21, 2010):

More examples:

<a href="{% url store.views.category_view category.slug %}{% query_string request "colour size brand sort_by show" "" %}">{{ category.name }}</a>

<a href="{{ url store.views.index }}{% query_string request "size colour brand price sort_by" "include_ampersand" %}show=100"{% ifequal show '100' %} class="current"{% endifequal %}>100</a>

<form action="{% url store.views.index %}" method="get" name="price_select_form" id="price_select_form"><div style="margin-top: 10px; margin-left: 15px;">{% query_string request "colour size brand sort_by show" "html_form" %}<div id="selected_prices_list_div"></div><img src="/static/buttons/apply.png" alt="Apply" id="price_select_link"></div></form>

#

Please login first before commenting.