day 82 URL分發
阿新 • • 發佈:2018-06-02
delet clas IT 分發 register disco 增刪改 ins 增刪改查
一 、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
案例
day 82 URL分發