python - django 中的模块化模板

标签 python html django templates

我正在开始使用 Django,我正在尝试制作一个模块化模板,但我不知道如何做。现在,我有以下文件:

1- base.html(提供所有网站的基本布局):

{% 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(主数据库读取)

{% 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 %}

最后,我想添加第三个文件,名为 menu.html,其中包含站点菜单。我想将它添加到 base.html 文件中。我一直在考虑通过以下方式进行操作,但我没有工作:

{% load 'menu.html' %}

非常感谢您的帮助!

最佳答案

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

文档:

关于python - django 中的模块化模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18230278/

相关文章:

javascript - 找不到我的 "addEventListener"函数的解决方案

javascript - Firefox 在选择时触发,而不是在更改时触发

django - 问 : List of dict access with mongoengine in django

python - Pandas 数据帧矢量化采样

python - Gaussian NB fit() 函数需要固定长度的向量

javascript - jQuery Accordion 不会删除单击上一个 Accordion 时的类

python - Django 在 View 之间传递数据

python - 使用 Python 在 PyQt4 中的 QPushButton 中设置文本颜色

Python 连接到 MySQL 失败,错误为 "dh key too small"

python - 如何查询描述非规范化表的多个 Django 模型