V
V
Villian_Os2020-07-15 06:33:22
Django
Villian_Os, 2020-07-15 06:33:22

How to pass some context to django using template extension?

Hello, I apologize in advance if I'm not explaining the problem clearly.
I have templates in my django project: main-page.html, help-page.html and base.html.

base.html is my navbar, menu bar.
In it, I implemented, in a nutshell, the following code.

<head>
    <title>{% block title%} {% endblock %}</title>
</head>
<body>
<nav>
<!-- Тут много строк кода навигационной панели, я его не вставил -->
Новых отчетов <span class="badge badge-pill badge-success">{{ new_report }}</span>
<!-- В "new_report " вставляется из views.py моя переменная о количестве отчетов. -->
  </nav>
 <main>
          {% block content%} {% endblock %}
   </main>
</body>


In main-page.html
{% extends 'main_application/base.html' %}

{% block title %}
Главная страница
{% endblock %}

{% block content %}
{{ description }}
{% endblock %}


In help-page.html
{% extends 'main_application/base.html' %}

{% block title %}
Справка
{% endblock %}

{% block content %}
Данный сайт предназначен для отчетности
{% endblock %}


That is, I use extends to pass content to those two pages.
And here's my problem: I passed the {{ new_report }} variable from view.py to base.html, but when I open main-page or help-page, it's simply not there. That is, with the help of extends, I can only transfer the html code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-07-15
@bacon

Read the docs, it's all there. Templates do not know how to pass context, they only accept it, for example, from a specific view. There are also context processors https://docs.djangoproject.com/en/3.0/ref/template... that pass context to all templates. This problem can also be solved via https://docs.djangoproject.com/en/3.0/howto/custom...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question