Django admin后台添加自定义菜单和功能页面

django admin是根据注册的模型来动态生成菜单,从这个思路出发,如果想添加自定义菜单,那就创建一个空模型并且注册。步骤如下:

1、创建空模型:

python 复制代码
class ResetSVNAuthFileModel(models.Model):
    """仅用来显示一个菜单"""

    class Meta:
        verbose_name = '重置授权文件'
        verbose_name_plural = verbose_name

2、注册到admin.py

重写changelist_view函数,使其点击菜单时,展示自定义的页面。在此你可以做orm查询,并且将数据渲染到模板中。而我想实现的是在页面中点击一个按钮来触发请求后端接口,来实现某种行为。

python 复制代码
from django.contrib import admin
from django.shortcuts import render

from apps.setting.models import ResetSVNAuthFileModel

@admin.register(ResetSVNAuthFileModel)
class FeedbackStatsAdmin(admin.ModelAdmin):
    def changelist_view(self, request, extra_content=None):
        template_name = 'reset_svn_auth_file.html'
        return render(request, template_name)
复制代码
reset_svn_auth_file.html:
html 复制代码
{% extends "admin/base_site.html" %}
{% load static %}

{% block content %}
    <div class="text-center">
        <!-- 点击按钮调用 JavaScript 函数 -->
        <button onclick="resetFunction()" id="resetButton" class="btn btn-primary btn-lg">重置授权文件</button>
    </div>
{% endblock %}

{% block extrahead %}
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        function resetFunction() {
            // 禁用按钮
            document.getElementById("resetButton").disabled = true;
            // 发送 AJAX 请求到 Django 视图
            fetch('/admin-api/reset-auth-file').then(response => {
                if (response.ok) {
                    Swal.fire({
                        icon: 'success',
                        title: 'OK',
                        text: '操作成功',
                    })
                } else {
                    response.json().then(data => {
                        console.log(data)
                        Swal.fire({
                            icon: 'error',
                            title: '重置失败',
                            text: data.msg,
                        })
                    })
                }
            }).catch((error) => {
                Swal.fire({
                    icon: 'error',
                    title: 'Oops...',
                    text: '请求失败',
                })
            }).finally(() => {
                // 恢复按钮状态
                document.getElementById("resetButton").disabled = false;
            });
        }
    </script>
{% endblock %}

把该文件放在空模型所在app下的templates文件夹。

3、定义后端路由和接口

django总路由:

python 复制代码
from django.contrib import admin
from django.urls import path, include

from apps.repository.views import ResetAuthFileView

admin_api = [
    path('reset-auth-file/', ResetAuthFileView.as_view(), name='reset-auth-file'),

]

urlpatterns = [
    path('admin/', admin.site.urls),
    path('admin-api/', include(admin_api)),  # 在admin 自定义页面中发送过来的请求
]

接口函数:

python 复制代码
class ResetAuthFileView(APIView):
    def get(self, request):
        # 可以写个中间件做认证
        sessionid = request.COOKIES.get('sessionid')
        csrftoken = request.COOKIES.get('csrftoken')

        session = SessionStore(session_key=sessionid)
        if not session.exists(session_key=sessionid):
            return Response(status=401)
        try:
            SVN.reset_authz_file()
        except Exception as e:
            return Response(data={'msg': f'重置失败:{e}'}, status=400)
        return Response(data={'msg': '重置完成'})

大功告成,看成果:

点击菜单后的页面:

相关推荐
安当加密20 小时前
MySQL数据库透明加密(TDE)解决方案:基于国密SM4的合规与性能优化实践
数据库·mysql·性能优化
筵陌20 小时前
深入理解 Reactor 反应堆模式:高性能网络编程的核心
服务器
JH307320 小时前
第七篇:Buffer Pool 与 InnoDB 其他组件的协作
java·数据库·mysql·oracle
板凳坐着晒太阳20 小时前
ClickHouse 配置优化与问题解决
数据库·clickhouse
数据库生产实战20 小时前
解析Oracle 19C中并行INSERT SELECT的工作原理
数据库·oracle
IT森林里的程序猿21 小时前
基于机器学习方法的网球比赛胜负趋势预测
python·机器学习·django
AAA修煤气灶刘哥21 小时前
服务器指标多到“洪水泛滥”?试试InfluxDB?
数据库·后端·面试
阿沁QWQ1 天前
MySQL服务器配置与管理
服务器·数据库·mysql
wanhengidc1 天前
云手机能够做些什么?
运维·服务器·人工智能·智能手机·云计算
2401_865854881 天前
腾讯云手机适用于哪些人群
服务器