【问题标题】:django include template from another appdjango 包含来自另一个应用程序的模板
【发布时间】:2011-06-02 02:00:06
【问题描述】:

在设置我的项目并努力保持应用程序不依赖时,我遇到了一个障碍。我希望来自不同应用程序的所有模板都具有一致的页眉和页脚。这是我正在尝试的:

myproject/
         base/
             templates/
                      header.html
                      footer.html
         app1/
             templates/
                      my_app1_page.html -> want to include 'header.html'
                                           and 'footer.html' from base app

假设还有更多的应用也想要这样做。这是可能的和/或正确的方法吗?

【问题讨论】:

    标签: django templates


    【解决方案1】:

    只要应用程序位于 INSTALLED_APPS 中并且启用了应用程序目录的模板加载器,您就可以包含来自其他应用程序的任何模板,即:

    {% include "header.html" %}
    

    ...因为您的模板直接位于应用程序的模板目录中。 通常,为了避免名称冲突,最好使用:

    app1/
        templates/
            app1/
                page1.html
                page2.html
    app2/
        templates/
            app2/
                page1.html
                page2.html
    

    还有{% include "app1/page1.html" %}{% include "app2/page1.html" %} ...

    但是:为了保持一致的外观和感觉,使用模板继承比包含要好得多。模板继承是 Django 模板系统的真正的好东西之一,只要有意义(大多数时候)选择继承而不是包含。

    我的建议:

    • 为您的项目创建一个基本模板(“base.html”是默认约定),其中包含页眉和页脚以及主要内容的 {%block content%}
    • 让您的其他模板继承表单 base.html {% extends "base.html" %} 并覆盖内容部分

    查看此问题的另一个回复以获取文档链接

    【讨论】:

    • 我只是想为刚接触 django 的人提供一些建议,他们可能认为包含函数是一种不好的做法:虽然我同意,使用继承确实很好,但不是说它比包含选项更好是非常正确的。它只是有不同的目的。它是处理较小的 html sn-ps(如 Wordpress 等流行 cms 中的模块/小部件)的好工具。例如,您可能希望在第二个应用程序中列出您博客中的所有类别 - 如果没有 {% include %} 选项,您可能会意识到您必须在两个应用程序中重写相同的代码。
    【解决方案2】:

    虽然您当然可以通过使用 include tag 并指定绝对路径来做到这一点,但在 Django 中工作的正确方法是使用 Template inheritance

    【讨论】:

    • 如果我有一些模型和部分模板来渲染该模型怎么办 - 例如地址,事件?我认为,在这种情况下,这种包容是有意义的。
    【解决方案3】:

    如果您使用“$ django-admin startproject project”启动一个项目,则会创建一个名为“project-folder-name”的文件夹,即 project/。添加一些应用程序并在“settings.py”中添加应用程序 -> INSTALLED_APPS=[..., app1, app2] 并在项目中创建一个模板文件夹后/我得到了这样的结构:

    project/
        project/
            templates/
                base.html
        app1/
            templates/
                app1/
                    page1.html
                    page2.html
        app2/
            templates/
                app2/
                    page1.html
                    page2.html
    

    在模板 app1/template/page1.html 我写的

    {% 扩展 'base.html' %}

    并出现“TemplateDoesNotExist at /”错误消息。

    然后我添加了另一个名为“core”的应用程序并将 base.html 添加到模板文件夹(在 settings.py 中编辑 INSTALLED_APPS),我现在得到了这个结构:

    project/
        project/
            templates/    (unused)
                base.html (not seen)
        core/
            templates/
                base.html
        app1/
            templates/
                app1/
                    page1.html
                    page2.html
        [...]
    

    现在错误信息消失了,base.html 找到了templates/app1/page1.html:

    {% extends 'base.html' %}
    

    您可以像这样更改文件夹结构:

        core/
            templates/
                core/
                    base.html
    

    那你需要把模板app1/page1.html改成

    {% extends 'core/base.html' %}
    

    也是。

    作为替代方案,您还可以在此说明“项目”中将“您的项目名称”添加到您的设置文件中,如下所示:

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'project', # your project name main folder
        'core',
        'App1',
        'App2',
    ]
    

    现在 django 也找到了 project/templates/base.html。但是我不知道是否推荐这种解决方案。

    project/
        project/
            templates/    
                base.html (now it is found)
    

    附:谢谢(我的赞成票还没有计算在内)如果这个答案在某种程度上清晰易懂,请评论我

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2016-03-22
      • 1970-01-01
      • 2018-02-02
      • 2018-03-24
      相关资源
      最近更新 更多