How can I get the variable passed to the included template in django

I am new to Django and cannot achieve anything trivial. Please help me with this.

  • I set the pgurl variable in my view.py
  • I can access the {{pgurl}} variable in my with_tag.html template. This template includes the pagination.html template.
  • In pagination.html I cannot use the variable {{pgurl}} and nothing is printed

How can I pass this variable to 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 %}
+3
source share
1 answer

, . , , . assert , .

0

Source: https://habr.com/ru/post/1723072/


All Articles