【问题标题】:Project not finding my reusable app's admin template override项目找不到我的可重用应用的管理模板覆盖
【发布时间】:2016-08-23 13:12:32
【问题描述】:

我有一个可重用的应用程序,其目录结构如下:

myapp/
  myapp/
    views.py
    models.py
    ...etc

    templates/
      myapp/
        template1.html
        ...etc
      admin/
        index.html
  test/
    ...testing stuff
  setup.py
  ...etc

我将覆盖 index.html 管理模板,以便我可以在 {% block userlinks %} 中添加一些其他链接,这些链接将在项目使用我的应用程序时出现在项目的导航中。

但是,当在项目中使用我的应用程序时,管理主页仍然使用 Django 自己的 index.html 文件。项目本身有一个它使用的base_site.html,但模板继承图(在django-debug-toolbar 中)如下所示:

/path/to/virtualenv/django/contrib/admin/templates/admin/index.html
/projectpath/projectname/templates/admin/base_site.html
/path/to/virtualenv/django/contrib/admin/templates/admin/base.html

...第一个条目应该是我的应用程序模板目录中的index.html 文件!有谁知道为什么找不到它?如果需要,我可以发布设置。

【问题讨论】:

    标签: python django django-templates django-admin


    【解决方案1】:

    Django 的模板加载器按照应用程序在INSTALLED_APPS 中定义的顺序查找模板。在您的情况下,您必须在您的应用程序之前定义 django.contrib.admin,因此 Django 将始终先查看那里并使用它找到的第一个模板。

    更改它,使您的应用在列表中排在第一位:

    INSTALLED_APPS = [
        'myapp',
        'django.contrib.admin',
        ...
    ]
    

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2017-02-19
      • 2014-12-09
      • 2013-01-23
      • 2019-05-02
      • 2012-07-14
      • 2019-01-22
      • 2018-10-22
      • 1970-01-01
      相关资源
      最近更新 更多