Django display a variable in another template

I have a django project, where I have two templates: notification.html and base.html. Using get_context_data() method I display the number of answers at the notification.html. I tried to use the same variable in base.html, but it didn't work. I also created CBV that and used get_context_data() to pass the same logic and display it at the base.html, but it doesn't work. How do I display the variable in another template?

I don't how to pass 'answers.count' to the base.html, if base.html doesn't have a view (I created this only now trying to display this variable).

forum.views

class NotificationView(ListView):
model = Answer
template_name = 'forum/notification.html'

# If user visited the notification page - mark all answers as read
def get_queryset(self):
    user = get_object_or_404(User, username=self.kwargs.get('username'))
    question = Question.objects.filter(user=user)
    answer_read = self.model.objects.filter(question__in=question)
    answer_read.update(is_read=True)
    return answer_read

def get_context_data(self, **kwargs):
    context = super().get_context_data()
    user = get_object_or_404(User, username=self.kwargs.get('username'))
    question = Question.objects.filter(user=user)
    context['answers_all'] = Answer.objects.filter(question__in=question).order_by('-date_posted')
    context['answers'] = Answer.objects.filter(question__in=question, is_read=False)
    return context

forum/notification.html

{% block content %}
<div class="container">
    <b>УNotofocations: {{ answers.count }}</b>
        {% for answer in answers_all %}
        <div class="card mb-3 mt-2">
            <div class="card-body">
                <a href="{% url 'detail' answer.question.id %}" class="mb-2">Ответ на вопрос: {{ answer.question }}</a>
                <p>{{ answer.detail }}</p>
                <a href="{% url 'users_questions' answer.user.username %}">{{ answer.user.username }}</a>
                <span class="ml-4">{{ answer.date_posted|naturalday }}</span>
            </div>
        </div>
        {% endfor %}
</div>
{% endblock %}

main.views

class BaseView(TemplateView):
template_name = 'main/base.html'

def get_context_data(self, **kwargs):
    context = super().get_context_data()
    user = get_object_or_404(User, username=self.kwargs.get('username'))
    question = Question.objects.filter(user=user)
    context['answers'] = Answer.objects.filter(question__in=question)
    return context

main/base.html

{% if user.is_authenticated %}
    {% if answers.count < 1 %}
        <a href="{% url 'notification' user.username %}" class="bell text-light mr-3" id="btn-bell"><i class="fas fa-bell"></i></a>
    {% else %}
        <a href="{% url 'notification' user.username %}" class="bell text-light mr-3" id="btn-bell"><i class="fas fa-bell"></i><span class="pending">{{ answers.count }}</span></a>
    {% endif %}
{% endif %}

guess you are only missing the build-in template tags

in forum/notification.html

{% extends "base.html" %} 

in main/base.html

{% block content %}{% endblock %}

https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#extends

https://docs.djangoproject.com/en/4.1/ref/templates/language/#template-inheritance

Finally, I solved this problem. The idea was to display the variable from context_data at base.html. As get_context_data didn't help, I found the solution in Django documentation. The solution was so easy, I hope to save time for other learners if they got the same problem.

I used context_processors.py to pass extra information and display it on the page. My step were:

  1. Created a context_processors.py file in my app directory.

  2. Created a function def get_notifications_number(request) where I filter what I want to display at base.html and returned return {'answers': answers}

  3. Finally, I registered the file in Django TEMPLATES 'my_app.context_processors.get_notifications_number'.

Back to Top