Django项目:CRM(客户关系管理系统)--12--05PerfectCRM实现King_admin注册功能获取内存01
1 #base_admin.py 2 #Django admin 注册功能的形式 3 # sites = { 4 # 'crm':{ 5 # 'customers':CustomerAdmin, 6 # 'customerfollowup':CustomerFollowUPAdmin, 7 # } 8 # } 9 10 class AdminRegisterException(Exception): #自定义异常 11 def __init__(self,msg): 12 self.message = msg 13 14 class BaseAdmin(object):#自定义方法 15 list_display = () 16 17 class AdminSite(object): 18 def __init__(self): 19 self.registered_sites = {} #传到views 里调用 20 def register(self,model,admin_class=None): #默认值None 使用 BaseAdmin 21 app_name = model._meta.app_label#用内置方法获取 APP名字 (crm) 22 model_name = model._meta.model_name#用内置方法获取 表名 (Customer) 23 if app_name not in self.registered_sites: 24 self.registered_sites[app_name] = {} #创建 crm={} 25 if model_name in self.registered_sites[app_name]: 26 raise AdminRegisterException("app [%s] model [%s] has already registered!异常" 27 %(app_name,model_name))#自定义异常, 28 if not admin_class: 29 admin_class = BaseAdmin #默认值None 使用 BaseAdmin 30 31 self.registered_sites[app_name][model_name] = admin_class #注册APP 32 33 site = AdminSite() #实例化类 单例模式 34 35 36 # registered_sites={} 37 # def register(model,admin_class=None): #默认值None 使用 BaseAdmin 38 # app_name = model._meta.app_label#用内置方法获取 APP名字 (crm) 39 # model_name = model._meta.model_name#用内置方法获取 表名 (Customer) 40 # if app_name not in registered_sites: 41 # registered_sites[app_name] = {} #创建 crm={} 42 # if model_name in registered_sites[app_name]: 43 # raise AdminRegisterException("app [%s] model [%s] has already registered!异常" 44 # %(app_name,model_name))#自定义异常 45 # if not admin_class: 46 # admin_class = BaseAdmin #默认值None 使用class BaseAdmin 47 # registered_sites[app_name][model_name] = admin_class #注册APP
site.register(models.Customer,CustomerAdmin)
site.register(models.CourseRecord)
1 #kingadmin.py 2 3 from crm import models 4 #print("kingadmin crm",models.Customer) 5 6 # from king_admin.base_admin import register,BaseAdmin 7 from king_admin.base_admin import site,BaseAdmin 8 9 #05客户信息表 10 class CustomerAdmin(BaseAdmin): 11 #显示的字段(不能包含ManyToManyField) 12 list_display= ('id','name','qq','consultant','source','consult_content','status','date') 13 14 15 16 # register(models.Customer,CustomerAdmin) 17 # register(models.CourseRecord) 18 19 site.register(models.Customer,CustomerAdmin) 20 site.register(models.CourseRecord)
print("registered_sites",base_admin.site.registered_sites)
return render(request, 'king_admin/app_index.html', {"site": base_admin.site})
1 #views.py 2 from django.shortcuts import render 3 4 # from django import conf #配置文件 5 # print("dj conf:",conf) #配置文件 6 # print("dj conf:",conf.settings)#配置文件.设置 7 8 from king_admin import app_config #自动调用 动态加载类和函数 9 10 # from king_admin.base_admin import registered_sites # registered_sites={} 11 from king_admin import base_admin 12 13 def app_index(request): 14 # for app in conf.settings.INSTALLED_APPS: 15 # print(app)#循环打印 配置文件.设置.安装应用程序#.Perfectcustomer\settings里的INSTALLED_APPS列表 16 17 # print("registered_sites",registered_sites) 18 # return render(request, 'king_admin/app_index.html') 19 20 # print("registered_sites", base_admin.registered_sites) 21 # return render(request, 'king_admin/app_index.html') 22 23 print("registered_sites",base_admin.site.registered_sites) 24 return render(request, 'king_admin/app_index.html', {"site": base_admin.site})
1 {#app_index.html#} 2 3 {% extends 'king_master/king_index.html' %} 4 5 {% block right-container-content %} 6 7 {#从base_admin传到views,views返回的值items()方法把字典转换成列表#} 8 {% for app,app_tables in site.registered_sites.items %} 9 10 <table class="table table-hover"> 11 <thead> 12 <tr> 13 <h3>{{ app }}</h3>{#循环APP名字#} 14 </tr> 15 </thead> 16 17 <tbody> 18 {#上层循环的变量值,items()方法把字典转换成列表#} 19 {% for model_name,admin_class in app_tables.items %} 20 <tr> 21 <th >{{ model_name }}{{ admin_class }} </th> 22 </tr> 23 {% endfor %} 24 </tbody> 25 </table> 26 {% endfor %} 27 28 {% endblock %}
您的资助是我最大的动力!
金额随意,欢迎来赏!
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的
因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【颜言】!