自定义 Django 管理模板 [英] Customize Django admin template

查看:41
本文介绍了自定义 Django 管理模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试像这样自定义导航栏

i tried customizing navbar like this

Base_site.html

 {% block nav-global %}
 <img class = "brand_img" src = "{% static 'images/ic_launcher.png'%}" 
    width = "50" height = "50" alt = "logo">
 {%block branding%}    

  {% endblock %}
  <div class = "head">
    <h1 id = "name">Admin Dashboard</h1>
  </div>    
{% endblock %}

看起来像这样

现在我尝试在 {%block branding%} 中添加登录页面的标题

now i try to add header for login page inside {%block branding%}

但如果我在品牌块内添加它也会显示在导航栏中,如果我尝试在品牌块中添加图像和标题图像将显示登录页面标题.

but if i add inside branding block it is displayed in navbar also and if i try to add both image and header in branding block image is displayed login page header.

如何为导航栏和登录页面标题添加不同的标题?

how to add different titles for navbar and login page header?

推荐答案

这很容易实现.

在您的 templates 文件夹中,您应该已经创建了一个 admin 子文件夹.在那里,您应该放置文件 base_site.htmllogin.html.

Inside your templates folder, you should have created an admin subfolder. Inside there, you should place the files base_site.html and login.html.

base_site.html的内容:

{% extends 'admin/base_site.html' %}

{% load static %}

{% block branding %}
    <div class="head">
        <h1 id="name">Admin Dashboard</h1>
    </div>
{% endblock %}

{% block nav-global %}
    <img class="brand_img" src="{% static 'images/ic_launcher.png'%}" width="50" height="50" alt="logo">
{% endblock %}

login.html的内容:

{% extends 'admin/login.html' %}

{% block branding %}
    <div class="head">
        <h1 id="name">Custom header text for LOGIN screen only</h1>
    </div>
{% endblock %}

以下是正确的项目结构:

Below is the correct project structure:

project/
    myapp/
    myapp2/
    project/
    templates/
        admin/
            base_site.html
            login.html
    manage.py

请注意您要覆盖的每个 html 模板中的 extends.这是至关重要的.有关更多信息,请查看 文档.

Please note the extends inside each html template you want to override. It's vital. For more info take a look at the docs.

这篇关于自定义 Django 管理模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆