扩展 django 管理模板时的特殊性
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过将原始的 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.
其他管理模板,例如。
change_form.html
覆盖breadcrumbs
块本身,因此您还需要在这些模板中覆盖它(=覆盖它们并在其中定义您的块)。The other admin templates, eg.
change_form.html
override thebreadcrumbs
block themselves, so you need to override it also in these templates (=override them and define your block in there).