You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django Admin模板中访问模型属性?(Django2.0.3+Python3.5.3)

简单优化Django 2.0.3 Admin仪表盘的实用方案

嘿,针对你用Python 3.5.3 + Django 2.0.3优化Admin后台主页的需求,我整理了几个简单易上手的方案,直接基于你现有的./templates/admin/index.html模板来改,不用复杂的第三方库:


1. 自定义模块排序,把常用功能放最前面

默认仪表盘是按App名称排序的,你可以手动调整显示顺序,把高频使用的模块(比如权限管理)放在最顶部:

{% extends "admin/base_site.html" %}
{% load i18n static %}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}"/>
{% endblock %}

{% block coltype %}colMS{% endblock %}
{% block bodyclass %}{{ block.super }} dashboard{% endblock %}

{% block content %}
<div id="content-main">
  <!-- 优先显示常用的权限管理模块 -->
  {% for app in app_list %}
    {% if app.name == "Authentication and Authorization" %}
      <div class="app-{{ app.app_label }} module">
        <table>
          <caption>
            <a href="{{ app.app_url }}" class="section" title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">{{ app.name }}</a>
          </caption>
          {% for model in app.models %}
            <tr class="model-{{ model.object_name|lower }}">
              {% if model.admin_url %}
                <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
              {% else %}
                <th scope="row">{{ model.name }}</th>
              {% endif %}
              {% if model.add_url %}
                <td><a href="{{ model.add_url }}" class="addlink">{% trans 'Add' %}</a></td>
              {% else %}
                <td>&nbsp;</td>
              {% endif %}
              {% if model.admin_url %}
                <td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
              {% else %}
                <td>&nbsp;</td>
              {% endif %}
            </tr>
          {% endfor %}
        </table>
      </div>
    {% endif %}
  {% endfor %}

  <!-- 显示剩余的其他模块 -->
  {% for app in app_list %}
    {% if app.name != "Authentication and Authorization" %}
      <div class="app-{{ app.app_label }} module">
        <table>
          <caption>
            <a href="{{ app.app_url }}" class="section" title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">{{ app.name }}</a>
          </caption>
          {% for model in app.models %}
            <tr class="model-{{ model.object_name|lower }}">
              {% if model.admin_url %}
                <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
              {% else %}
                <th scope="row">{{ model.name }}</th>
              {% endif %}
              {% if model.add_url %}
                <td><a href="{{ model.add_url }}" class="addlink">{% trans 'Add' %}</a></td>
              {% else %}
                <td>&nbsp;</td>
              {% endif %}
              {% if model.admin_url %}
                <td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
              {% else %}
                <td>&nbsp;</td>
              {% endif %}
            </tr>
          {% endfor %}
        </table>
      </div>
    {% endif %}
  {% endfor %}
</div>
{% endblock %}

2. 添加自定义快捷操作入口

直接在主页顶部加常用操作的快捷按钮,不用每次点进模块找:

<!-- 在{% block content %}的最开头插入这段代码 -->
<div class="module" style="margin-bottom: 2rem; padding: 1rem;">
  <h2>{% trans 'Quick Actions' %}</h2>
  <ul style="list-style: none; padding-left: 0;">
    <li style="margin-bottom: 0.5rem;">
      <a href="{% url 'admin:auth_user_add' %}" class="addlink">{% trans 'Add New User' %}</a>
    </li>
    <li>
      <a href="{% url 'admin:blog_post_changelist' %}" class="changelink">{% trans 'Manage Blog Posts' %}</a>
    </li>
  </ul>
</div>

提示:URL格式是admin:<app_label>_<model_name>_add或admin:<app_label>_<model_name>_changelist,你可以根据自己的模型名称调整。


3. 轻量美化样式,让界面更清爽

在{% block extrastyle %}里添加自定义CSS,不用替换原有样式,只做局部优化:

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/dashboard.css" %}"/>
<style>
  /* 调整模块卡片样式,加个阴影更现代 */
  .module {
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    border: none;
    margin-bottom: 1.5rem;
  }
  /* 优化标题颜色和字体 */
  #content-main h2 {
    font-size: 1.2rem;
    color: #2c3e50;
    margin-bottom: 0.8rem;
  }
  /* 调整链接 hover 效果 */
  .section:hover {
    color: #2980b9 !important;
  }
</style>
{% endblock %}

4. 隐藏不需要的模块

如果有些系统自带或第三方App你不想在仪表盘显示,直接在模板里过滤掉:

{% for app in app_list %}
  <!-- 排除debug_toolbar和django_celery_results这两个App -->
  {% if app.app_label not in 'debug_toolbar,django_celery_results' %}
    <!-- 原有的模块显示代码 -->
  {% endif %}
{% endfor %}

要是只想隐藏某个模型(比如系统日志),可以在模型循环里加判断:

{% for model in app.models %}
  {% if model.name != 'Log entry' %}
    <!-- 原有的模型行代码 -->
  {% endif %}
{% endfor %}

内容的提问来源于stack exchange,提问作者koddr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:12:12