5

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!

4

2 回答 2

4

而不是使用{% load 'menu.html' %}你必须使用{% include 'menu.html' %}

文件:

于 2013-08-14T11:24:51.760 回答
0

正确的方法是使用包含模板标签

{% include 'menu.html' %}

其中包括一个模板并使用当前上下文呈现它。

注意:当你遇到麻烦时,django docs是最好的去处!永远记住这一点

于 2013-08-14T11:20:22.867 回答