【问题标题】:Modular templates in djangodjango 中的模块化模板
【发布时间】:2013-08-16 07:40:26
【问题描述】:

我开始使用 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' %}

非常感谢您的帮助!

【问题讨论】:

标签: python html django templates


【解决方案1】:

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

文档:

【讨论】:

    【解决方案2】:

    正确的方法是使用include templatetag

    {% include 'menu.html' %}
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-21
      • 2018-11-25
      • 2016-04-13
      • 2014-01-16
      • 2012-12-19
      • 2010-12-26
      • 2016-01-06
      • 1970-01-01
      相关资源
      最近更新 更多