D
D
daho0052020-06-02 21:54:02
Django
daho005, 2020-06-02 21:54:02

How to pass context to include in all Django Python pages?

Greetings. I have an HTML wrapper that other templates inherit from.

<html>
<head>
<title>Сайт на Python</title>
</head>
<body>
{% include 'includes/top_menu.html' with context=context %}
</body>
</html>

There is a top_menu.html file in which pages from the database should be drawn in a cycle, which I add from the admin panel.
It is necessary for me somehow that on all pages in top_menu.html the context with this query-set was transferred.
In general, this can be done every time passing the context to views, here is an example:
from .models import Page
def index(request):
     context={'pages': Page.objects()}
     return render(request, 'shablon.html', context=context)
def feedback(request):
     context={'pages': Page.objects()}
     return render(request, 'shablon2.html', context=context)
def help(request):
     context={'pages': Page.objects()}
     return render(request, 'shablon3.html', context=context)

But this is not very practical. How can I optimize this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
noremorse_ru, 2020-06-02
@daho005

context_processors

R
rm root, 2020-06-02
@rm_root

did you try that?

from .models import Page

context={'pages': Page.objects()}

def index(request):
     return render(request, 'shablon.html', context)
def feedback(request):
       return render(request, 'shablon2.html', context)
def help(request):
      return render(request, 'shablon3.html', context)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question