My initial template base.html must load menus with items dynamically loaded at the beginning. Is it possible? Some hints?
You can use template inheritance which is provided by Django. But in your case suppose you have two different templates with names navbar.html
and base.html
and you want to add navbar.html
to beginning of your base.html
. With Django tag (i.e. {% include 'navbar.html' %}
) you can include your navbar.html
content in your base.html
. Just like following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Try Django</title>
</head>
<body>
{% include 'navbar.html' %}
</body>
</html>
In other scenario if you want to add something to your base.html
(for example product_list.html
) you can use Django tag (i.e. {% extends 'base.html' %}
) in your destination template but do not forget to use {% block content %}
and {% endblock content %}
in your base.html
(for example in the body tag of base.html
and then use {% block content %}
and {% endblock content %}
in your destination template. Finally just add your codes between that block contents of yours. For more information check the following codes:
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Try Django</title>
</head>
<body>
{% include 'navbar.html' %}
{% block content %}{% endblock content %}
</body>
</html>
product_list.html
{% extends 'base.html' %}
{% block content %}
{% for obj in object_list %}
{{ obj.id }} - <a href="{{ obj.get_absolute_url }}">{{ obj.name }}</a><br>
{% endfor %}
{% endblock content %}
And if you need something else just go to Django template docs.