为什么我无法覆盖Django管理模板?

3

我无法更改我的Django管理模板。我已经按照Django文档中的说明进行了操作,并且已经查看了StackOverflow上的相关问题。

我已经创建了/templates/admin/my_app/目录,并在/templates/admin/my_app/base_site.html中更改了base_site.html文件。

settings.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

base_site.html:

#base_site.html
{% extends "admin/base_site.html" %}

{% block title %}{{ title }} | {{ site_title|default:_('NEW TITLE') }}{% 
endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ 
site_header|default:_('NEW TITLE') }}</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

文件结构:

my_project
└── my_project
    └── templates
        └── admin
           └── my_app
                └── base_site.html

为什么我无法覆盖Django Admin模板?

尝试通过按下Ctrl+F5来刷新页面。 - undefined
仍然无法正常工作 - undefined
2个回答

2

base_site.html放置在/templates/admin文件夹中,如下所示:

/templates/admin/base_site.html而不是/templates/admin/my_app/base_site.html

提示:Original Answer应该翻译成"最初的回答"

我以为我已经尝试了所有的方法,最终这个解决了问题。我正在使用 Django 2.2 版本,记录一下,并且没有其他 Stack Overflow 的答案能够解决,也不是 Django 2.2 文档中所建议的 OP 结构。 - undefined

1

base_site.html 只能在 /templates/admin/ 中自动覆盖,而不能在 /templates/admin/<app>//templates/admin/<app>/<model>/ 中自动覆盖。

根据我的实验和文档,在您的虚拟环境中,django admin 模板位于 django/contrib/admin/templates/admin/,可以在 /templates/admin//templates/admin/<app>//templates/admin/<app>/<model>/ 中自动覆盖。* 在 /templates/admin/<app>/<model>/ 中覆盖 *app_index.html* 是没有意义的:

actions.html  
app_index.html # *Not meaningful to override in `/templates/admin/<app>/<model>/`  
change_form.html  
change_form_object_tools.html  
change_list.html  
change_list_object_tools.html  
change_list_results.html  
date_hierarchy.html  
delete_confirmation.html
delete_selected_confirmation.html
object_history.html  
pagination.html  
popup_response.html  
prepopulated_fields_js.html  
search_form.html  
submit_line.html

根据我的实验,你可以在虚拟环境下自动覆盖位于django/contrib/admin/templates/admin/的Django管理模板,只需将其放置在/templates/admin/目录中。
app_list.html
base.html
base_site.html
filter.html
index.html
login.html
nav_sidebar.html

根据我的实验,你的虚拟环境中的django管理模板在django/contrib/admin/templates/admin/auth/user/下只能自动覆盖到/templates/admin/auth/user/
add_form.html
change_password.html

根据我的实验,django管理模板在您的虚拟环境下只能自动覆盖/templates/admin/edit_inline/中的django/contrib/admin/templates/admin/edit_inline/

stacked.html
tabular.html

根据我的实验,您的虚拟环境中 django/contrib/admin/templates/admin/includes/ 中的 Django 管理模板只能自动覆盖在 /templates/admin/includes/ 中。
fieldset.html
object_delete_summary.html

根据我的实验,你可以在虚拟环境下手动覆盖位于django/contrib/admin/templates/admin/widgets/的Django管理模板。只需将其放置在/templates/*目录下即可。
clearable_file_input.html
foreign_key_raw_id.html
many_to_many_raw_id.html
radio.html
related_widget_wrapper.html
split_datetime.html
url.html

我不知道为什么,就是按照你描述的方式完美运作。我正在使用Django 4.2。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接