xadmin系列之啟動、註冊、分發
阿新 • • 發佈:2018-11-10
from django.conf.urls import url from django.shortcuts import HttpResponse class xadmin_class(object): def __init__(self,model,site): self.model = model self.site = site def list_view(self,request): return HttpResponse("xadmin----->list_view") def add_view(self,request): return HttpResponse("xadmin----->add_view") def change_view(self,request,cid): return HttpResponse("xadmin----->change_view----->{cid}".format(cid = cid)) def del_view(self,request,did): return HttpResponse("xadmin----->list_view------->{did}".format(did = did)) def geturlsoperation(self): temp = [] temp.append( url(r'^$', self.list_view) ) temp.append( url(r'^add/$', self.add_view) ) temp.append( url(r'^(?P<cid>\d+)/change/$', self.change_view) ) temp.append( url(r'^(?P<did>\d+)/del/$', self.del_view) ) return temp,None,None class xadminsite(object): def __init__(self,name="admin"): self._register = {} # def list_view(self,request): # # return HttpResponse("xadmin----->list_view") # # def add_view(self,request): # # return HttpResponse("xadmin----->add_view") # # def change_view(self,request,cid): # return HttpResponse("xadmin----->change_view----->{cid}".format(cid = cid)) # # def del_view(self,request,did): # return HttpResponse("xadmin----->list_view------->{did}".format(did = did)) # def geturlsoperation(self): # temp = [] # temp.append( # url(r'^$', self.list_view) # ) # temp.append( # url(r'^add/$', self.add_view) # ) # temp.append( # url(r'^(?P<cid>\d+)/change/$', self.change_view) # ) # temp.append( # url(r'^(?P<did>\d+)/del/$', self.del_view) # ) # return temp,None,None def get_urls(self): temp = [] for model, admin_class_obj in self._register.items(): # 通過表的物件獲取表的名稱 model_name = model._meta.model_name # 通過表的物件獲取表的app的名稱 app_name = model._meta.app_label temp.append( url(r'^{app_name}/{model_name}/'.format(app_name=app_name, model_name=model_name),admin_class_obj.geturlsoperation()) ) print(temp) return temp def register(self,model,admin_class=None,**kwargs): if not admin_class: admin_class = xadmin_class self._register[model] = admin_class(model,self) @property def urls(self): return self.get_urls(),None,None site = xadminsite()