Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modular templates in django

I am getting started with Django, and I'm trying to make a modular template, but I don't know how. Now, I have the following files:

1- base.html (which provides basic layout for all the website):

{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<title>My site</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/bootsrap.min.css' %}" />
</head>

<body>
<h1>Test title</h1>
{% block content %}
{% endblock %}
</body>
</html>

2- index.html (main db read)

{% extends 'base.html' %}
{% block content %}
{% if latest_smartphones_list %}
<ul>
{% for s in latest_smartphones_list %}
    <li><a href="#">{{ s.brand }} {{ s.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No smarphones available.</p>
{% endif %}
{% endblock %}

Finally, i wanted to add a third file, called menu.html which would contain the site menu. I wanted to add it in the base.html file. I've been thinking about doing it in the following way, but i doesn't work:

{% load 'menu.html' %}

Thanks so much for your help!

like image 213
Paolo Avatar asked Oct 04 '22 06:10

Paolo


1 Answers

Instead of using {% load 'menu.html' %} you have to use {% include 'menu.html' %}

Docs:

  • include
  • load
like image 172
sandeep Avatar answered Oct 07 '22 20:10

sandeep