Paolo
Paolo

Reputation: 361

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!

Upvotes: 5

Views: 1801

Answers (2)

sandeep
sandeep

Reputation: 3345

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

Docs:

Upvotes: 4

Samuele Mattiuzzo
Samuele Mattiuzzo

Reputation: 11046

The correct way is using the include templatetag

{% include 'menu.html' %}

which includes a template and renders it with the current context.

NB: whenever you are in trouble, django docs is the best place to go to! Always keep this in mind

Upvotes: 0

Related Questions