Задать вопрос

Тел: +7 965 3737 888

497

Просмотров

6

Ответов

Append paramaters to a GET querystring (template tag)

<p>This tag is designed to facilitate pagination in the case where both the page number and other parameters (eg. search criteria) are passed via GET. </p>
<p>It takes one argument - a dictionary of GET variables to be added to the current url</p>
<p>Example usage:</p>
{% for page_num in results.paginator.page_range %}     
&lt;a href="{% append_to_get p=page_num %}"&gt;{{ page_num }}&lt;/a&gt;
{% endfor %}

<p>Note that the passed arguments are evaluated within the template context.</p>

Вопрос полезен? Да0/Нет0
file_2496.py(1.4Кб)
None

Ответы (6):

Ответperoksid:16.12.2012
Ответ полезен? Да0/Нет0

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())

Ответkneufeld:29.04.2011
Ответ полезен? Да0/Нет0

You need to have the request context processor.

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

Ответquietbob:19.07.2010
Ответ полезен? Да0/Нет0

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:19.07.2010
Ответ полезен? Да0/Нет0

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.

Ответudfalkso:29.04.2010
Ответ полезен? Да0/Нет0

Thanks! I was just about to do something similar.

Ответgregb:13.07.2009
Ответ полезен? Да0/Нет0

Oops, thanks for pointing that out.