发布于 2026-01-06 0 阅读
0

Django 管理后台定制 - 黑色仪表盘

Django 管理后台定制 - 黑色仪表盘

各位程序员朋友们,大家好!

本文介绍了如何使用由Black Dashboard开发的免费且现代的 UI 工具包来定制默认的 Django 管理界面Creative-Tim

最终软件包可直接从 Github 下载(MIT 许可证),同时还提供另一个使用相同设计的 Django Starter:Django Black Dashboard

链接

Django Admin Black - 现代 Django 管理界面模板,动画演示。


Django 默认管理员

作为一个“内置电池”的框架,Django 默认提供了一组丰富的页面,这些页面通常用于所有现代 Web 应用程序:登录、注册、更改密码、错误页面(404、500 页面)、用于分页和管理表格的小部件以及管理部分中使用的其他资源。

我们可以通过访问 Django 的安装目录来查看所有默认页面。以下截图仅展示了 Django 自带的部分默认页面和组件:

.../site-packages/django/contrib/admin/templates/
├── admin/
│   │
│   ├── auth/
│   │   └── user/
│   │       ├── add_form.html
│   │       └── change_password.html
│   │
│   ├── 404.html
│   ├── 500.html
│   ├── actions.html
│   ├── app_index.html
│   ├── base.html
│   ├── base_site.html
│   ├── index.html
│   ├── invalid_setup.html
│   ├── login.html
│   ├── pagination.html
│   ├── popup_response.html
│   └── submit_line.html
└── registration/
    ├── logged_out.html
    ├── password_change_done.html
    ├── password_change_form.html
    ├── password_reset_complete.html
    ├── password_reset_confirm.html
    ├── password_reset_done.html
    ├── password_reset_email.html
    └── password_reset_form.html
Enter fullscreen mode Exit fullscreen mode

要自定义任何默认页面,我们需要创建自己的模板目录,在父目录中使用相同的名称和位置创建文件,并通知 Django 使用它。

为了更深入地了解,我们将自定义 404 错误页面并配置 Django 来使用它。开始吧!


自定义 404 页面

如上所述,第一步是创建模板目录:

# Django Root Project <-- you are here
mkdir -p templates/
Enter fullscreen mode Exit fullscreen mode

步骤 2 - 更新 Django 设置

要使用新模板,应按如下方式更新项目设置文件:

# core/settings.py
# ...

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",

        # Add the templates directory to the DIR option:
        "DIRS": [os.path.join(BASE_DIR, "templates"), ], # <- New Line
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]
Enter fullscreen mode Exit fullscreen mode

当应用程序请求模板文件时,Django 会首先扫描用户在设置文件中定义的目录来查找模板文件。如果找不到,则会使用site-packages/django/contrib/admin/templates/ directory默认位置的默认版本。


步骤 3 - 自定义 404 页面

要自定义 404 页面,只需将默认版本从目录复制并保存到步骤 2admin/templates/中创建的目录中即可。

# Django Root Project <-- you are here
vi templates/404.html
Enter fullscreen mode Exit fullscreen mode
<!-- templates/404.html --> 

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

{% block title %}{% trans 'Page not found' %}{% endblock %}

{% block content %}

<!-- The updated line -->
<h2>{% trans 'Page not found' %} - SOMETHING Custom</h2>

<p>{% trans "We're sorry, but the requested page could not be found." %}</p>

{% endblock %}
Enter fullscreen mode Exit fullscreen mode

一旦我们保存了该文件,当用户与我们的应用程序交互时发生 404 错误情况时,Django 将使用该文件。


Django Admin Black

只需在终端中输入几个命令,即可在任何 Django 项目中使用此免费示例,之后更新文件settings即可使用新界面。


如何使用它

更改 Django 项目内部的目录。如果您还没有 Django 项目,可以使用Django Dashboard Black作为母项目。

# YOU should be in the ROOT project
$ pip install git+https://github.com/app-generator/django-admin-black.git
$ # or
$ easy_install git+https://github.com/app-generator/django-admin-black.git
Enter fullscreen mode Exit fullscreen mode

将应用程序添加admin_blackINSTALLED_APPSDjango 项目 settings.py 文件中(注意它应该在 'django.contrib.admin' 之前):

    INSTALLED_APPS = (
        ...
        'admin_black.apps.AdminBlackConfig',
        'django.contrib.admin',
    )
Enter fullscreen mode Exit fullscreen mode

迁移数据库

$ python manage.py migrate admin_black
$ # or
$ python manage.py syncdb
Enter fullscreen mode Exit fullscreen mode

清除浏览器缓存,启动应用程序并访问管理模块。新的用户界面应该就能正常显示了:


Django Black 管理后台 - 控制面板屏幕

Django Black 管理后台 - 控制面板屏幕。


Django Black 管理后台 - 创建用户屏幕

Django Black Admin - 创建用户屏幕。


Django Black 管理后台 - 编辑用户权限

Django Black Admin - 编辑用户权限。


感谢您阅读到这里!您可以通过以下链接找到更多资源:

文章来源:https://dev.to/sm0ke/django-admin-customization-black-dashboard-301c