Answer the question
In order to leave comments, you need to log in
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
{% 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})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question