views:

188

answers:

1

Hi,

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 %}
A: 

It'll 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 the variables if they were passed correctly.

Pydroid
The page is a paginated one and shows up correctly. What Im trying to acheive is as follows. I have a page example.com/tag/python/?page=1 and i have changed that to example.com/tag/python/1 and done the changes in urls.py and views.py and set a variable for the current page url sans page as pgurl which is example.com/tag/python/ . All works fine and this variable is passed into the template for the view which is with_tag.html but the variable is anaccessible at the pagination template where I want to use the var to create next/back links- {{pgurl}}/{{ page_obj.next_page_number }}{{ getvars }}
Orca