Say Goodbye to Monotony: Revamping the Django Admin Homepage
Introduction Previously I built a Django project, and to make the admin backend more visually appealing, I customized Django's (or rather, SimpleUI's) default Admin homepage. You can see the details in this article: Project Complete - Based on Django 3.x - Development and Deployment Notes The previous two articles on Django 3.x development and deployment notes covered databases, APIs, and performance. I noticed some friends left comments expressing interest in the admin homepage customization, so I'm writing this article to introduce it~ Analysis The customization is based on my custom
目录8 个章节
Introduction
Previously I built a Django project, and to make the admin backend more visually appealing, I customized Django's (or rather, SimpleUI's) default Admin homepage. You can see the details in this article: Project Complete - Based on Django 3.x - Development and Deployment Notes
The previous two articles on Django 3.x development and deployment notes covered databases, APIs, and performance. I noticed some friends left comments expressing interest in the admin homepage customization, so I'm writing this article to introduce it~
Analysis
The customization is based on my custom DjangoStarter development template. The code in this article will be synced to GitHub. Project URL: https://github.com/Deali-Axy/DjangoStarter
DjangoStarter's Admin uses SimpleUI, which is an Admin theme based on Vue and ElementUI. It uses ElementUI's tab component to implement multi-tab functionality. This component itself supports custom homepages, so my approach is to write a new page using Django Templates, configure the route, and then replace SimpleUI's default homepage.
The default homepage looks like this — a bit monotonous

After the makeover, it looks like this

Hey, it's a bit better than before, and the content is richer~
Next, let's implement this homepage makeover step by step.
Dependencies
There are plenty of admin templates online. Domestic ones like ElementUI and Ant Design are all quite usable, but looking good requires knowing CSS~~(unfortunately I'm not very good at it)~~
So I turned my attention to open-source components from abroad, and AdminLTE is pretty great — it extends Bootstrap, which I'm most familiar with, looks good and is simple and easy to pick up~
Let's get started then
The dependencies used by this page are as follows
- Currently the AdminLTE version is 3.2, based on Bootstrap 4.6
- Charts use chart.js, version 3.8
- Icons use Font Awesome, using the free 6.0 version
Just copy the dependencies section of package.json:
"dependencies": {
"@fortawesome/fontawesome-free": "^6.0.0",
"admin-lte": "3.2",
"bootstrap": "^4.6.1",
"chart.js": "^3.8.0",
"jquery": "^3.6.0",
}
Copy it, save, then run the yarn command to install the dependencies~
PS: I recommend using yarn to manage npm packages, though you can of course use npm directly
Modifying the Gulp Task
For using and managing frontend assets in Django, you can refer to this article: Introducing NPM and gulp to Manage Frontend Assets in a Django Project
//使用 npm 下载的前端组件包
const libs = [
{name: "admin-lte", dist: "./node_modules/admin-lte/dist/**/*.*"},
{name: "chart.js", dist: "./node_modules/chart.js/dist/**/*.*"},
{name: "jquery", dist: "./node_modules/jquery/dist/**/*.*"},
{name: "bootstrap", dist: "./node_modules/bootstrap/dist/**/*.*"},
];
// 使用 npm 下载的前端组件,自定义存放位置
const customLibs = [
{name: 'font-awesome', dist: './node_modules/@fortawesome/fontawesome-free/**/*.*'},
]
After saving, run gulp move in the project root directory~
Start Writing the Page
We create extend_home.html under the templates/admin directory
I won't paste the actual code here; it's already uploaded to GitHub. You can see it here: https://github.com/Deali-Axy/DjangoStarter/blob/master/templates/admin/extend_home.html
For the charts I used fake data to make a pie chart. The data looks like this
let exampleData = [
{label: 'a', value: 10},
{label: 'b', value: 10},
{label: 'c', value: 10},
{label: 'd', value: 10},
{label: 'e', value: 10},
{label: 'f', value: 10},
]
The text for news and quick actions uses Django's built-in random text generation tag
{% lorem 6 w random %}
For specific usage, see the official docs: https://docs.djangoproject.com/zh-hans/4.0/ref/templates/builtins/#lorem
PS: All the content on this page is fake data. In actual use, you can pass data via context or request an API to fill in real data~
After finishing the page template, save it
Next, just configure the route
Write the View
I put this page in DjangoStarter's default App
Edit the apps/core/views.py file
Add a new function
# 扩展admin主页,美化后台
def extend_admin_home(request):
return render(request, 'admin/extend_home.html')
Configure the routes
urlpatterns = [
# ...
path('admin_home', views.extend_admin_home),
]
Configure the SimpleUI Homepage
Edit config/settings.py
Add this line of code in the SimpleUI configuration section
SIMPLEUI_HOME_PAGE = f'/{URL_PREFIX}core/admin_home'
PS: Because DjangoStarter added a URL prefix feature, you need to include
URL_PREFIX
Done
That's it. The actual code is fairly long; you can check out the GitHub project: https://github.com/Deali-Axy/DjangoStarter
If you don't want to follow the steps one by one, feel free to try my DjangoStarter template — everything is already integrated, ready to use out of the box~
_EOF_
Say Goodbye to Monotony: Revamping the Django Admin Homepage
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
使用django-treebeard实现树类型存储与编辑
无非继承MP_Node配置TreeAdmin,拖拽编辑树节点。可视化?也就django-treebeard能用,其他要么停更要么功能拉胯。初始化数据?手动导入,否则admin连按钮都没有,bug。接口输出无非path和depth,自己实现更舒服。
2024-08-11 · Django · 约 1 分钟
轻量级消息队列Django-Q轻度体验
前言 最近做的这个项目(基于Django),需要做个功能,实现定时采集车辆定位。 这让我想起来几年前那个OneCat项目,当时我用的是Celery这个很重的组件 Celery实在是太重了,后来我做公众号采集平台的时候,又接触了Django-RQ和Django-Q这俩,前者是对RQ的封装,让RQ和Django更好的结合在一起;后者是一个全新的「多进程任务队列」组件,相比起celery很轻量,当时使用
2022-08-31 · Django · 约 1 分钟
Django+Taro项目实现企业微信登录
前言 还是最近在做的一个小项目,后端用的是Django搭配RestFramework做接口,前端第一次尝试用京东开源的Taro框架来做多端(目前需要做用于企业微信的H5端和微信小程序) 本文记录一下企业微信登录的流程,上周看文档看得头晕晕的,其实很简单,封装好了就几行代码的事~ 两种方式 一种是先拼接好登录要用的地址authorize_url,回调地址设置成h5应用的网页入口,然后把authori
2022-04-08 · Django · 约 1 分钟
给Django-Admin添加验证码和多次登录尝试限制
Django自带的Admin很好用,但是放到生产环境总还差了点什么= = 看看admin的介绍: Django奉行Python的内置电池哲学。它自带了一系列在Web开发中用于解决常见问题或需求的额外的、可选工具。这些工具和插件,例如django.contrib.redirects都必须在settings中的INSTALLED_APPS处进行注册,有的还需要执行manage.py migrate命令
2021-12-18 · Django · 约 1 分钟
讨论
共 0 条加载评论中…