Django自定义增删改查组件

目标:增删改查组件

知识储备:
1.如何使用django自带admin

  • models.py 创建类
  • admin.py 注册
    admin.site.register(models.UserInfo)
  • urls.py 路由配置
    admin -> admin.site.urls

2.路由系统

/index/             index,name=n1
/index/(\d+)/       index,name=n2
/web/               include('app01.urls')

反向查找

views.py中:
  reverse('n1')
  reverse('n2',args=(666,))

模板中:
  {% url 'n1' %}
  {% url 'n2' 666 %}

3.文件加载
第一次导入后,再次导入时不再重新加载

4.面向对象(封装,继承)
示例:

class Base(object):
    def __init__(self,val):
        self.val = val

    def func(self):
        self.test()
        print(self.val)

    def test(self):
        print('base.test')


class Foo(Base):
    def test(self):
        print('foo.test')


class Bar(object):
    def __init__(self):
        self._registry = {}
    def register(self,a,b=None):
        if not b:
            b = Base
        self._registry[a] = b(a)

b = Bar()
b.register(1,Foo)
b.register(2)

print(b._registry)  #{1: <__main__.Foo object at 0x000002BE711D6278>, 2: <__main__.Base object at 0x000002BE711D62B0>}
b._registry[1].func()  # foo.test  1
b._registry[2].func()  # base.test  2

django.admin的路由规则:

            /admin/app01/role/           查看角色列表
            /admin/app01/role/add/       添加角色
            /admin/app01/role/2/change/  编辑
            /admin/app01/role/2/delete/  删除
            
            
            /admin/app01/userinfo/          
            /admin/app01/userinfo/add/      
            /admin/app01/userinfo/1/change/

如何使用:
http://www.jianshu.com/p/c09be21c7ece
原理:
路由关系 + 对应视图函数
admin源码流程
admin.py
1.运行程序,找到每一个app中的admin.py文件,并加载
2.创建admin.site中的对象site = AdminSite()
3.执行对象的register方法,目的:讲注册类添加到_registry
admin.site是一个对象(单例模式创建),其中封装了:

_registry = {
    models.Role: ModelAdmin(models.Role,admin.site),
    models.UserInfo: ModelAdmin(models.UserInfo,admin.site)
    models.UserType: ModelAdmin(models.UserType,admin.site)
}

urls.py
再次调用 admin.site 对象的 urls属性:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

源码中的两个关键类:

class ModelAdmin(object):
    def __init__(self, model_class, site):
        self.model_class = model_class
        self.site = site

    def changelist_view(self, request):
        data_list = self.model_class.objects.all()
        return HttpResponse('列表页面')

    def add_view(self, request):
        return HttpResponse('添加页面')

    def delete_view(self, request, nid):
        return HttpResponse('删除页面')

    def change_view(self, request, nid):
        return HttpResponse('修改页面')

    def get_urls(self):
        urlpatterns = [
            url(r'^$', self.changelist_view),
            url(r'^add/$', self.add_view),
            url(r'^(.+)/delete/$', self.delete_view),
            url(r'^(.+)/change/$', self.change_view),
        ]
    return urlpatterns

    @property
    def urls(self):
        return self.get_urls()
class AdminSite(object):
    def __init__(self):
        self._registry = {}

    def register(self, model_class, model_admin):
        self._registry[model_class] = model_admin(model_class, self)

    def get_urls(self):
        """
        models.Role: ModelAdmin(models.Role,admin.site),
        models.UserInfo: ModelAdmin(models.UserInfo,admin.site)
        models.UserType: ModelAdmin(models.UserType,admin.site)
        models.Article: ModelAdmin(models.Article,admin.site)
        """
        url_list = []
        for model_class, model_admin in self._registry.items():
            app_name = model_class._meta.app_label
            model_name = model_class._meta.model_name
            url_list += [
                url('%s/%s' % (app_name, model_name,), include(model_admin.urls))
            ]

        return url_list

    @property
    def urls(self):
        return (self.get_urls(), None, None)

启动文件
路由系统
单例模式
面向对象继承和封装

你可能感兴趣的:(Django自定义增删改查组件)