开发者

How can I get a variable passed into an included template in django

I am a Django newbie and am unable to achieve something trivial. Please 开发者_如何学编程help me with this.

  1. I am setting a variable pgurl in my views.py
  2. Am able to access the variable {{pgurl}} in my with_tag.html template. This template includes a pagination.html template into itself
  3. In pagination.html I am unable to use the variable {{pgurl}} and nothing is printed

How can I get this variable passed into the included template?

views.py

def with_tag(request, tag, template_name='main/with_tag.html', current_page=1, pgurl=''):
    if request.method == 'GET':
        query_tag = Tag.objects.get(name=tag)
        primes = TaggedItem.objects.get_by_model(Prime, query_tag)
        primes = primes.order_by('-date')
        request.page = current_page
        tcm_pp = TCM_ITEMS_PER_PAGE
        pgurl = request.path
    else:
        return HttpResponseRedirect(request.path)

    return direct_to_template(request, template_name, { 'primes' : primes, 'prime_total' : Prime.objects.count(), 'now': datetime.now(), 'page' : current_page, 'tcm_pp' : tcm_pp, 'tag' : tag, 'pgurl' : pgurl })

with_tag.html

{% extends "base.html" %}
{% load comments %}
{% load pagination_tags %}

...

  {% include "pagination.html" %}
  {% paginate %}

pagination.html

{% if is_paginated %}
{% load i18n %}

<div class="pagination">
    {% if page_obj.has_previous %}
        <a href="{{ pgurl }}{{ page_obj.previous_page_number }}{{ getvars }}" class="prev">&lsaquo;&lsaquo; {% trans "previous" %}</a>
    {% else %}
        <span class="disabled prev">&lsaquo;&lsaquo; {% trans "previous" %}</span>
    {% endif %}
    {% for page in pages %}
        {% if page %}
            {% ifequal page page_obj.number %}
                <span class="current page">{{ page }}</span>
            {% else %}
                <a href="{{ pgurl }}{{ page }}{{ getvars }}" class="page">{{ page }}</a>
            {% endifequal %}
        {% else %}
            ...
        {% endif %}
    {% endfor %}
    {% if page_obj.has_next %}
        <a href="{{ pgurl }}{{ page_obj.next_page_number }}{{ getvars }}" class="next">{% trans "next" %} &rsaquo;&rsaquo;</a>
    {% else %}
        <span class="disabled next">{% trans "next" %} &rsaquo;&rsaquo;</span>
    {% endif %}
</div>
{% endif %}


It will be helpful if you post the output of the rendered page. The context should get passed, might be your template tags instead. Try to do assert and check to see if the variables were passed correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜