Django中urls.py中的分发,自己写Xadmin替代admin(83)

分发

from django.contrib import admin
from django.urls import path

from django.shortcuts import HttpResponse

def test(request):
    return HttpResponse('test')

def test01(request):
    return HttpResponse('test01')
def test02(request):
    return HttpResponse('test02')
def test03(request):
    return HttpResponse('test03')
def test04(request):
    return HttpResponse('test04')
def test05(request):
    return HttpResponse('test05')

urlpatterns = [
    path('admin/', admin.site.urls),

  #结构固定,第二个和第三个固定为None        叫做一级分发
    path('yuan/', ([
                       path('test01/', ([
                           #二级分发
                            path('test04/', test04),
                            path('test05/', test05),
                                        ],None,None)),
        
                       path('test02/', test02),
                       path('test03/', test03),
                   ],None,None)),
]

 

 

 自己写的Xadmin

from django.contrib import admin
from django.urls import path,re_path

from django.shortcuts import HttpResponse

def test(request):
    return HttpResponse('test')

def test01(request):
    return HttpResponse('test01')
def test02(request):
    return HttpResponse('test02')
def test03(request):
    return HttpResponse('test03')
def test04(request):
    return HttpResponse('test04')
def test05(request):
    return HttpResponse('test05')

#查
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(re_path(
'^$',list_view)) temp.append(re_path('^add/$',add_view)) temp.append(re_path('^(\d+)/change/$',change_view)) temp.append(re_path('^(\d+)/delete/$',delete_view)) return temp def get_urls(): print(admin.site._registry) temp = [] for model,admin_class_obj in admin.site._registry.items(): model_name = model._meta.model_name  #获得模块的名字 app_name = model._meta.app_label    #获得APP的名字 temp.append(path('{0}/{1}/'.format(app_name,model_name), (get_urls_2(),None,None)),)    #二级分发  一共实现了28条url return temp urlpatterns = [ path('admin/', admin.site.urls), path('Xadmin/', (get_urls(),None,None)),  #一级分发 ]

 自己写Xadmin替代admin

具体看day83  Xadmin

Xadmin.service.Xadmin

from django.urls import path,re_path
from django.shortcuts import HttpResponse

#默认样式类
class ModelXadmin(object):
    def __init__(self,model,site):
        self.model = model
        self.site = site
        

class XadminSite(object):
    def __init__(self, name='admin'):
        self._registry = {}

    # 查
    def list_view(self,request):
        return HttpResponse('list_view')

    # 增
    def add_view(self,request):
        return HttpResponse('add_view')

    # 改
    def change_view(self,request, id):
        return HttpResponse('change_view')

    # 删
    def delete_view(self,request, id):
        return HttpResponse('delete_view')

    def get_urls2(self):
        temp = []

        temp.append(re_path('^$', self.list_view))
        temp.append(re_path('^add/$', self.add_view))
        temp.append(re_path('^(\d+)/change/$', self.change_view))
        temp.append(re_path('^(\d+)/delete/$', self.delete_view))

        return temp

    @property
    def urls2(self):
        return self.get_urls2(),None,None
    
    def get_urls(self):
        print("get_urls中的:",self._registry)
        temp = []
        for model, admin_class_obj in self._registry.items():
            model_name = model._meta.model_name
            app_name = model._meta.app_label
            temp.append(path('{0}/{1}/'.format(app_name, model_name), self.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)

site = XadminSite()
View Code

 小知识

  model_name = model._meta.model_name  #获得模块的名字
  app_name = model._meta.app_label    #获得APP的名字

  

 

 

 

  obj = Book._meta.get_field('title')

  print(obj.verbose_name)          #可以获得我们给属性取的值

posted @ 2020-06-10 17:08  my_Sunday  阅读(221)  评论(0编辑  收藏  举报