kingadmin后台主页面开发

Posted fqh202

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kingadmin后台主页面开发相关的知识,希望对你有一定的参考价值。

功能需求

能显示所有已注册的app 和 其内部的model表。页面如下图所示:

技术分享图片

index.html

仅显示内容区域,导航栏忽略。

div class="container-fluid">
      <div class="row">
        <div class="col-sm-9 col-md-8 main">
            <!--遍历每个app,取出每个注册的app名称 和 对应的model表结合-->
            {% for app_name,app_tables in enabled_admins.items %}
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th style="background: #2e6da4;color: white;font-size: 20px" colspan="2">
                                <!--取出app-->
                                <a href="{% url 'kingadmin_app_page' app_name %}"
                                    style="color: whitesmoke">{{ app_name }}</a></th>
                        </tr>
                    </thead>
                    <tbody>
                        <!--遍历当前app下的每个model表-->
                        {% for model_name in app_tables %} <!---->
                        <tr>
                            <td><a href="{% url 'model_obj_list' app_name model_name %}">{{ model_name }}</a></td>
                            <td><a href="{% url 'add_obj' app_name model_name %}"
                                    ><span class="glyphicon glyphicon-plus">增加</span></a></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            {% endfor %}
        </div>

      </div>
    </div>

enabled_admins推导步骤

site.enabled_admins={
    'crm': {
        'customerinfo': <crm.kingadmin.CustomerInfoAdmin object at 0x04FB43B0>, 
            # admin_class.model = model_class
        'role': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4410>, 
        'userprofile': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4450>, 
        'course': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4B70>, 
        'courserecord': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4A30>,
        'student': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4BB0>, 
        'classlist': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4A10>, 
        'studyrecord': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4A50>,
        'branch': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4D50>, 
        'studentenrollment': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4D70>, 
        'contracttemplate': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4D90>, 
        'menu': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4DB0>, 
        'customerfollowup': <king_admin.base_king_admin.BaseKingAdmin object at 0x04FB4DD0>}, 
    
    'student': {
        'homework': <student.kingadmin.HomeworkAdmin object at 0x04FB4E30>, 
        'homeworkrecord': <student.kingadmin.HomeworkRecordAdmin object at 0x04FB4DF0>}
}

1、kingadmin/sites.py

from king_admin.base_king_admin import BaseKingAdmin  # 表字段配置


class AdminSite(object):
    def __init__(self):
        # 8、外部调用则生成全局字典,且只实例化一次
        self.enable_admins = {}

    def register(self, model_class, admin_class=BaseKingAdmin):
        # 1、获取注册表的 app名(字符串)
        app_name = model_class._meta.app_label  # role

        # 2、获取注册表的 model名(字符串)
        model_name = model_class._meta.model_name  # crm

        # 3、实例化admin_class配置
        if not admin_class:
            # 没有传入则默认实例化BaseKingAdmin类,内部也配置了相关字段,但内容为空
            admin_class = BaseKingAdmin()
        else:
            # 实例化自定义ModelAdmin
            admin_class = admin_class()

        # 4、将 admin_class 与 model_class 关联起来,方便取数据
        admin_class.model = model_class

        # 5、由于一个app下可能有多个model,对于首次注册的model,创建app的key
        if app_name not in self.enable_admins:
            self.enable_admins[app_name] = {}
        
        # 6、将所有的app的model放在同一个字典中
        self.enable_admins[app_name][model_name] = admin_class


# 7、实例化,共外部引用
site = AdminSite()

2、app/kingadmin.py

传入model以及model表的自定义配置信息(可以不传)就调用步骤1中生成enable_admins全局变量:

from crm import models
from king_admin.base_king_admin import BaseKingAdmin


class CustomerInfoAdmin(BaseKingAdmin):
    """自定义表配置信息"""
    list_display = ['name','contact_type', 'status', 'source','consultant','date']
    list_filter = ['contact_type','consultant','status','source','date']
    search_fields = ['name','consultant__username']
    filter_horizontal = ['consult_course']


# 9、调用并生成全局字典
# enable_admins = {'crm':{'customerinfo':CustomerInfoAdminObj}}
site.register(models.CustomerInfo, CustomerInfoAdmin)

3、kingadmin/app_setup.py

定义函数 能找到并执行所有app目录下的kingadmin.py文件:

from django import conf


def kingadmin_auto_discover():
    """导入app.kingadmin 模块文件,即为kingadmin.py"""
    for app_name in conf.settings.INSTALLED_APPS:
        try:
            mod = __import__('%s.kingadmin'%app_name)
            print(mod.kingadmin)
        except ImportError:
            pass

4、kingadmin/views.py

from king_admin import app_setup

app_setup.kingadmin_auto_discover()

from king_admin.sites import site

以上是关于kingadmin后台主页面开发的主要内容,如果未能解决你的问题,请参考以下文章

前后端分离开发vue+Elementyui+abpcorewebapi商城管理系统后台主页面

kingadmin后台新增页面开发

kingadmin后台删除对象页面开发

共享单车—— React后台管理系统开发手记:主页面架构设计

kingadmin后台怎么使用kingadmin模块

ASP.NET网站,怎么配套开发后台管理系统?