一 、admin 流程
(1) 启动
autodiscover_modules('admin', register_to=site)
(2) 注册
单例模式
admin.site=AdminSite(): 单例对象
class AdminSite(object):
def __init__(self, name='admin'):
self._registry = {}
def register(self, model, admin_class=None, **options):
if not admin_class:
admin_class = ModelAdmin
self._registry[model] = admin_class(model, self) # {Book:ModelAdmin(Book)}
admin.site.register(Book) # admin.site._registry={Book:ModelAdmin(Book)}
admin.site.register(Publish) # admin.site._registry={Book:ModelAdmin(Book),Publish:ModelAdmin(Publish)}
class Authoconfig(admin.ModelAdmin):
pass
admin.site.register(Author,Authoconfig) # admin.site._registry={Book:ModelAdmin(Book),Publish:ModelAdmin(Publish),Author:Authoconfig(Author)}


二 、设计URL
为每个app下的model设计增删改查4个URL,以book为例。
127.0.0.1:8000/admin/app01/book/
127.0.0.1:8000/admin/app01/book/add
127.0.0.1:8000/admin/app01/book/1/change/
127.0.0.1:8000/admin/app01/book/1/delete
三 、URL分发
案例
二级路由
from django.conf.urls import url
from django.contrib import admin
from app01 import views
from django.shortcuts import HttpResponse
def test01(request):
return HttpResponse('TEST01')
def test02(request):
return HttpResponse('TEST02')
def test03(request):
return HttpResponse('TEST03')
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^query/',views.query),
url('^yuan/',([
url('^test01/',test01),
url('^test02/',test02),
url('^test03/',test03),
],None,None))
]



三级路由
def test001(request):
return HttpResponse('TEST001')
def test002(request):
return HttpResponse('TEST002')
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^query/',views.query),
url('^yuan/',([
url('^test01/',([
url('^test001/', test001),
url('^test002/', test002),
],None,None)),
url('^test02/',test02),
url('^test03/',test03),
],None,None))

四、URL 注册
需求: 为每个app下的
def test002(request):
return HttpResponse('TEST002')
def get_urls():
temp =[]
temp.append(url("app01/book",test002))
print(temp)
return temp
print(temp)
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^Xadmin/',([get_urls(),],None,None)),
url(r'^Xadmin/',(get_urls(),None,None)),
url(r'^query/',views.query),
#
# url('^yuan/',([
# url('^test01/',([
#
# url('^test001/', test001),
# url('^test002/', test002),
# ],None,None)),
# url('^test02/',test02),
# url('^test03/',test03),
# ],None,None))
]

结果:

第二种方法 。
def list_view(request): return HttpResponse('list_view') def add_view(request): return HttpResponse('add_view') def change_view(request): return HttpResponse('change_view') def delete_view(request): return HttpResponse('delete_view') def get_url2(): temp =[] temp.append(url(r'^$',list_view)) temp.append(url(r'^add/$',add_view)) temp.append(url(r'^(d+)/change/$',change_view)) temp.append(url(r'^(d+)/delete/$',delete_view)) def get_urls(): print(admin.site._registry) temp =[] for model,admin_class_obj in admin.site._registry.items(): app_name = model.meta.app_label model_name = model.meta.model_name temp.append(url(r'^{0}/{1}/'.format(app_name,model_name),(get_url2(),None,None)),) return temp
类 的名字 如何写:

类的app名字 :

输出的结果:

五 、URL的分发
def list_view(request):
return HttpResponse("list_view")
def add_view(request):
return HttpResponse("add_view")
def change_view(request,id):
return HttpResponse("change_view")
def delete_view(request,id):
return HttpResponse("delete_view")
def get_urls_2(): temp=[] temp.append(url(r"^$",list_view)) temp.append(url(r"^add/$",add_view)) temp.append(url(r"^(d+)/change/$",change_view)) temp.append(url(r"^(d+)/delete/$",delete_view)) return temp def get_urls(): print(admin.site._registry) # {Book:modelAdmin(Book),.......} temp=[] for model,admin_class_obj in admin.site._registry.items(): app_name=model._meta.app_label model_name=model._meta.model_name temp.append(url(r'^{0}/{1}/'.format(app_name,model_name), (get_urls_2(),None,None)),) return temp
admin 文件
from django.contrib import admin
# Register your models here.
from .models import *
from django.utils.safestring import mark_safe
class BookConfig(admin.ModelAdmin):
def deletes(self):
return mark_safe('<a href =''> 删除</a>')
list_display = ["title", "price", "publishDate", 'publish', deletes]
list_display_links = ["price"]
list_filter = ["price", "title", "authors", "publish"]
list_editable = ["title", ]
search_fields = ["title", "price"]
admin.site.register(Book,BookConfig)
print('1====>',admin.site._registry)
admin.site.register(Publish)
admin.site.register(Author)
admin.site.register(AuthorDetail)
print('2====>',admin.site._registry)
打印出来的结果
1====> {<class 'django.contrib.auth.models.Group'>: <django.contrib.auth.admin.GroupAdmin object at 0x00000257050332B0>, <class 'django.contrib.auth.models.User'>: <django.contrib.auth.admin.UserAdmin object at 0x0000025705055CF8>, <class 'app01.models.Book'>: <app01.admin.BookConfig object at 0x000002570506B9B0>}
2====> {<class 'django.contrib.auth.models.Group'>: <django.contrib.auth.admin.GroupAdmin object at 0x00000257050332B0>, <class 'django.contrib.auth.models.User'>: <django.contrib.auth.admin.UserAdmin object at 0x0000025705055CF8>, <class 'app01.models.Book'>: <app01.admin.BookConfig object at 0x000002570506B9B0>, <class 'app01.models.Publish'>: <django.contrib.admin.options.ModelAdmin object at 0x000002570506B978>, <class 'app01.models.Author'>: <django.contrib.admin.options.ModelAdmin object at 0x000002570506BA20>, <class 'app01.models.AuthorDetail'>: <django.contrib.admin.options.ModelAdmin object at 0x000002570506BA58>}

六、自定义Xadmin 之注册
service 目录下的xadmin文件
from django.conf.urls import url
from django.shortcuts import HttpResponse,render,redirect
class ModelXadmin(object):
def __init__(self,model,site):
self.model=model
self.site=site
def list_view(self, request):
print("self.model",self.model)
data_list=self.model.objects.all()
print("data_list",data_list)
return render(request, 'list_view.html',{"data_list":data_list})
def add_view(self, request):
return render(request, 'add_view.html')
def change_view(self, request, id):
return render(request, 'change_view.html')
def delete_view(self, request, id):
return render(request, 'delete_view.html')
def get_urls2(self):
temp = []
temp.append(url(r"^$", self.list_view))
temp.append(url(r"^add/$", self.add_view))
temp.append(url(r"^(d+)/change/$", self.change_view))
temp.append(url(r"^(d+)/delete/$", self.delete_view))
return temp
@property
def urls2(self):
return self.get_urls2(), None, None
class XadminSite(object):
def __init__(self, name='admin'):
self._registry = {}
def get_urls(self):
print(self._registry) # {Book:modelAdmin(Book),.......}
temp = []
for model, admin_class_obj in self._registry.items():
app_name = model._meta.app_label
model_name = model._meta.model_name
temp.append(url(r'^{0}/{1}/'.format(app_name, model_name), admin_class_obj.urls2), )
'''
url(r"app01/book",ModelXadmin(Book,site).urls2)
url(r"app01/publish",ModelXadmin(Publish,site).urls2)
url(r"app02/order",ModelXadmin(Order,site).urls2)
'''
return temp
@property
def urls(self):
return self.get_urls(),None,None
def register(self, model, admin_class=None, **options):
if not admin_class:
admin_class = ModelXadmin
self._registry[model] = admin_class(model, self) # {Book:ModelAdmin(Book),Publish:ModelAdmin(Publish)}
site=XadminSite()
app01 下的xadmin文件
from django.contrib import admin
# Register your models here.
from Xadmin.service.Xadmin import site,ModelXadmin
print("app01 Xadmin")
from app01.models import *
class BookConfig(ModelXadmin):
list_display=["title","prcie"]
site.register(Book,BookConfig)
class BookConfig(ModelXadmin):
list_display=["name"]
site.register(Publish,BookConfig)
site.register(Author)
site.register(AuthorDetail)
app02 下的xadmin文件
from Xadmin.service.Xadmin import site
from app02.models import *
site.register(Order)
site.register(Food)
print("_registry",site._registry)
views文件
from django.conf.urls import url from django.contrib import admin from Xadmin.service.Xadmin import site urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^Xadmin/', site.urls), ]