Django项目:CRM(客户关系管理系统)--13--05PerfectCRM实现King_admin注册功能获取内存02
admin_class.model = model
self.registered_sites[app_name][model_name] = admin_class #注册APP
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 32 33 # self.registered_sites[app_name][model_name] = admin_class #注册APP 34 35 #registered_sites {'crm': {'customer': <class 'crm.kingadmin.CustomerAdmin'>, 'courserecord': <class 'kingadmin.base_admin.BaseAdmin'>}} 36 #把类名放到class的对象里,然后通过class的对象传到前端 37 admin_class.model = model 38 self.registered_sites[app_name][model_name] = admin_class #注册APP 39 40 site = AdminSite() #实例化类 单例模式 41 42 43 # registered_sites={} 44 # def register(model,admin_class=None): #默认值None 使用 BaseAdmin 45 # app_name = model._meta.app_label#用内置方法获取 APP名字 (crm) 46 # model_name = model._meta.model_name#用内置方法获取 表名 (Customer) 47 # if app_name not in registered_sites: 48 # registered_sites[app_name] = {} #创建 crm={} 49 # if model_name in registered_sites[app_name]: 50 # raise AdminRegisterException("app [%s] model [%s] has already registered!异常" 51 # %(app_name,model_name))#自定义异常 52 # if not admin_class: 53 # admin_class = BaseAdmin #默认值None 使用class BaseAdmin 54 # registered_sites[app_name][model_name] = admin_class #注册APP
<th >{{ admin_class.mode }}{{ admin_class }} </th>
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 <th >{{ admin_class.mode }}{{ admin_class }} </th> 23 </tr> 24 {% endfor %} 25 </tbody> 26 </table> 27 {% endfor %} 28 29 {% endblock %}
您的资助是我最大的动力!
金额随意,欢迎来赏!
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的
因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【颜言】!