Login

Template tag which gets specific GET variables from the current request

Author:
aruseni.magiku
Posted:
March 21, 2010
Language:
Python
Version:
1.1
Tags:
get template-tag request variables get-variables
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. Manipulate URL query strings using context variables using a template tag by JHsaunders 4 years, 7 months ago
  2. A GET string modifier templatetag by cogat 6 years, 5 months ago
  3. Url query string template tag (qurl) by pricco 1 year, 8 months ago
  4. Url persistance of GET variables by alex_ndc 6 years, 6 months ago
  5. Simple template tag to do |stringformat filter with format from a variable by leopd 4 years, 1 month 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.