Project not finding my reusable app's admin template override

假如想象 提交于 2020-01-25 12:33:12

问题


I have a reusable app with a directory structure like this:

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

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

I'm overriding the index.html admin template so that I can add some additional links in {% block userlinks %} that will appear in a project's navigation when it uses my app.

However, when using my app inside a project, the admin homepage still uses Django's own index.html file. The project itself has a base_site.html that it uses, but the template inheritance diagram (in django-debug-toolbar) looks like this:

/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

...that first entry should be the index.html file in my app's templates directory! Does anyone know why it's not being found? I can post settings if needed.


回答1:


Django's template loader looks for templates in the order that apps are defined in INSTALLED_APPS. In your case you must have defined django.contrib.admin ahead of your app, so Django will always look there first and use the first template it finds.

Change it so that your app is first in the list:

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


来源:https://stackoverflow.com/questions/39102242/project-not-finding-my-reusable-apps-admin-template-override

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!