扩展 django 管理模板时的特殊性

发布于 2024-09-27 15:49:58 字数 879 浏览 3 评论 0原文

在我的 django 网站上,我决定只使用 UI 的管理模板,但我做了一些调整,例如网站名称、颜色等。甚至我的自定义视图也只是扩展 admin/base_site.html 我通过创建 templates/ 来做到这一点admin/base_site.html 包含以下代码:

{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}{{ title }} | {% trans 'Company Name' %}{% endblock %}

{% block extrastyle %}
<style>
#header{ background-color: #a67d3d; border-bottom: solid 3px #f5deb3; }
#branding h1{ color: #fff; }
</style>
{% endblock %}

{% block branding %}
<h1 id="site-name">{% trans 'My company' %}</h1>
{% endblock %}

{% block breadcrumbs %}
    {% include "breadcrumb.html" %}
{% endblock %}

整个管理站点都有我的新标题和颜色。但是,您可以看到我尝试用我自己的 breadcrumb.html(其中包含自定义导航栏)替换面包屑栏。这仅适用于扩展 admin/base_site.html 的自定义视图。普通的管理视图不会取代面包屑(但它们确实有新的颜色、公司名称等)。我不明白为什么这一块不起作用?此外,我有一些自定义的change_form.html 文件。这些也有样式更改,但没有自定义导航栏。但是,如果我在这些页面中放入面包屑块,它就会在这些页面上正常显示。

On my django site, I decided to just use the admin templates for the UI, but I made a few tweaks like the site name, color, etc. even my custom views just extend admin/base_site.html I did this by creating templates/admin/base_site.html with the following code:

{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}{{ title }} | {% trans 'Company Name' %}{% endblock %}

{% block extrastyle %}
<style>
#header{ background-color: #a67d3d; border-bottom: solid 3px #f5deb3; }
#branding h1{ color: #fff; }
</style>
{% endblock %}

{% block branding %}
<h1 id="site-name">{% trans 'My company' %}</h1>
{% endblock %}

{% block breadcrumbs %}
    {% include "breadcrumb.html" %}
{% endblock %}

The entire admin site has my new title and colors. However, you can see I tried replacing the breadcrumbs bar with my own breadcrumb.html (which contains a custom nav bar). This only works on custom views that extend admin/base_site.html. the normal admin views don't replace the breadcrumbs (but they do have the new colors, company title, etc.). I can't figure out why this one piece isn't working? Moreover, I have a few custom change_form.html files. These also have the style changes, but no custom nav bar. But, if I put in the breadcrumbs block in these pages, it shows up just fine on those pages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2024-10-04 15:49:58

我通过将原始的 base.html 文件复制到项目的“/templates/admin/”文件夹中来解决这个问题,删除了面包屑块,添加了“mynav”块,将我的导航栏放在那里。这样,我的导航栏就会显示在所有页面上,并且当较低的页面尝试放入面包屑时,它们不会受到任何阻碍,并且不会显示。

我不喜欢这样做,但我想不出其他方法。 lazerscience 建议的方法是可行的,但我必须在每个模板(change_form、change_list 等)中包含一个内容。对于其他人,我应该提到,有一个“nav-global”块,但我的导航栏使用列表/css/jscript 来显示滑出菜单,如果我将其放入该块中,这些菜单不会显示,不确定到底为什么。

I worked around this by copying the original base.html file into my project's '/templates/admin/' folder, deleted the breadcrumbs block, added a "mynav" block, put my navbar there. This way my nav bar shows up on all pages, and when the lower pages try to put in a breadcrumb there's no block for them and it doesn't show up.

I don't like doing it this way but i can't think of another way. The way suggested by lazerscience would work, but I'd have to do an include in every single template (change_form, change_list, etc.). For others, i should mention, there is a "nav-global" block, but my navbar uses lists/css/jscript to display slideout menus and these menus weren't showing up if i put it in that block, not sure exactly why.

鸠书 2024-10-04 15:49:58

其他管理模板,例如。 change_form.html 覆盖 breadcrumbs 块本身,因此您还需要在这些模板中覆盖它(=覆盖它们并在其中定义您的块)。

The other admin templates, eg. change_form.html override the breadcrumbs block themselves, so you need to override it also in these templates (=override them and define your block in there).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文