V
V
Villian_Os2020-08-06 12:00:03
Django
Villian_Os, 2020-08-06 12:00:03

How to pass a variable to a template that extends another template?

Hello. I use a template extension in django with the following code:
{% extends 'main_application/base.html' %}
In the base.html template, I wrote {{ request.user }} so that the username is displayed in other templates. This is how it works, but how can I make it so that I pass the variable from views.py to base.html in the same way or in some other way?
For example, I need to extract a variable from the database with the name of the organization in which the user is located and insert it into base.html.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Tikhonov, 2020-08-07
@Villian_Os

{% extends %}gets the same context as the main template. If you want to use the request variable in the base or child template, you need to pass the request to the context when rendering the template
render(template, {"request": request})

A
Anton Belokurov, 2020-08-06
@kyern

Add it to context

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question