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

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

问题描述

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



Base_site.html

  {%block nav-global%} 
< img class = brand_img src = {%static'images / ic_launcher.png'%}
width = 50高度= 50 alt =徽标>
{%block branding%}

{%endblock%}
< div class = head>
< h1 id =名称>管理控制台< / h1>
< / div>
{%endblock%}

看起来像这样





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



但是如果我在品牌块中添加它会显示在导航栏中



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



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

p>

解决方案

这很容易实现。



在您的<$ c内部$ c> templates 文件夹,您应该已经创建了 admin 子文件夹。在其中,您应该放置文件 base_site.html login.html



base_site.html 的内容:

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

{%load static%}

{%block branding%}
< div class =头
< h1 id = name>管理信息中心< / h1>
< / div>
{%endblock%}

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

login.html

  {%扩展了'admin / login.html'%} 

{%block branding%}
< div class = head>
< h1 id = name>仅用于登录屏幕的自定义标题文本< / h1>
< / div>
{%endblock%}

以下是正确的项目结构:

 项目/ 
myapp /
myapp2 /
项目/
模板/
管理员/
base_site.html
login.html
manage.py

请注意,您要覆盖的每个html模板中的扩展。至关重要有关更多信息,请参见 docs


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 %}

which looks like this

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?

解决方案

This can be achieved pretty easily.

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

Contents of 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 %}

Contents of 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

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天全站免登陆