D
D
DarkByte20152017-09-12 21:22:09
Django
DarkByte2015, 2017-09-12 21:22:09

How to organize template inheritance?

I have a base template base.html, header.html is inherited from it, almost all the rest are inherited from it. I pass styles and scripts through inheritance like this:
in base.html

<script type="text/javascript" src="{% static 'js/base.js' %}"></script>
{% block scripts %}{% endblock %}

and in other views I redefine it
{% block scripts %}<script type="text/javascript" src="{% static 'js/header.js' %}"></script>{% endblock %}

The problem is that this works in the header, but in more nested views it starts to override the styles and scripts of the header. Maybe we need to organize it somehow differently?
And one more question. In the header, I display the username with a link to the profile. Almost all views are inherited from the header, and I have to pass the user everywhere like this:
in header.html
<div>{% block username %}{% endblock username %}</div>

in nested views everywhere
{% block username %}<a href="{% url 'accounts:profile' %}">{{ user.username }}</a>{% endblock username %}

+ it is necessary to transfer to each user. Is it possible to do something differently? So that the model of the user who made the request (request.user) is automatically thrown everywhere?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-09-12
@DarkByte2015

In the heirs of header.html

{% block scripts %}
{{ block.super }}
<script type="text/javascript" src="{% static 'js/footer.js' %}"></script>
{% endblock %}

Instead {{ block.super }}, the contents of the parent block will be substituted.
And the username may or may not be displayed by the context processor .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question