[英]Django: Extending base.html in django admin
I've a base.html file which has vertical and horizontal menu-bar:我有一个具有垂直和水平菜单栏的 base.html 文件:
Wherever I want to use that I just simply write:无论我想在哪里使用它,我只是简单地写:
{% extends 'base.html' %}
{% block content %}
//html code
{% endblock content %}
But I don't know how to use the same file base.html
from templates directory in djando admin.但我不知道如何使用 djando admin 中模板目录中的相同文件base.html
。
I want output like this:我想要这样的输出:
What I Tried:我试过的:
How to override and extend basic Django admin templates? 如何覆盖和扩展基本的 Django 管理模板?
How do I correctly extend the django admin/base.html template? 如何正确扩展 django admin/base.html 模板?
Override Navbar in Django base admin page to be same as the base.html 将 Django 基本管理页面中的导航栏覆盖为与 base.html 相同
I tried few other solution just don't want to increase the length of question and base.html
file's code just has basic bootstrap, html code for menus.我尝试了一些其他解决方案,只是不想增加问题的长度, base.html
文件的代码只有基本的引导程序,菜单的 html 代码。
I am new to Django, little explanation would be highly appreciated!我是 Django 的新手,不胜感激!
What you are looking is similar to nav-global
.您正在查找的内容类似于nav-global
。
Try this:尝试这个:
First create a folder in your templates
folder as admin
and create a html file( base_site.html
) in the same folder首先以admin
身份在templates
文件夹中创建一个文件夹,并在同一文件夹中创建一个 html 文件( base_site.html
)
Assuming you have separate html file for menu-bars(Let's say the file is nav.html
).假设您有单独的菜单栏 html 文件(假设文件是nav.html
)。
Write the below code in base_site.html
:在base_site.html
编写以下代码:
{% extends 'admin/base.html' %}
{% block nav-global %}
{% include 'nav.html' %} #Your navigation html file
{% endblock %}
Unrelated to question: I found a git repo which will give you idea how to customize the django-admin menu.与问题无关:我找到了一个git repo ,它可以让您了解如何自定义 django-admin 菜单。
You can just extend the admin's base template as您可以将管理员的基本模板扩展为
{% extends "admin/base.html" %}
For example:例如:
{% extends "admin/base.html" %}
{% block sidebar %}
{{ block.super }}
<div>
<h1>Extra links</h1>
<a href="/admin/extra/">My extra link</a>
</div>
{% endblock %}
Also, make sure that you have added the admin
app to the INSTALLED_APPS
另外,请确保您已将admin
应用程序添加到INSTALLED_APPS
INSTALLED_APPS = [
# other apps,
'django.contrib.admin',
# other apps,
]
I had the same issue about a year and a half ago and I found a nice template loader on djangosnippets.org that makes this easy.大约一年半前,我遇到了同样的问题,我在 djangosnippets.org 上找到了一个不错的模板加载器,它使这变得容易。 It allows you to extend a template in a specific app, giving you the ability to create your own admin/index.html that extends the admin/index.html template from the admin app.它允许您在特定应用程序中扩展模板,使您能够创建自己的 admin/index.html,从管理应用程序扩展 admin/index.html 模板。 Like this:像这样:
{% extends "admin:admin/index.html" %}
{% block sidebar %}
{{block.super}}
<div>
<h1>Extra links</h1>
<a href="/admin/extra/">My extra link</a>
</div>
{% endblock %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.