Django-Xadmin
2019-03-13 本文已影响0人
Yanl__
1.启动
2.注册
3.设计URL
启动
-
启动Django项目时,加载app模块(在setting.py文件中)
image.png -
加载Xadmin app中apps.py模块里的XadminConfig类
image.png
from django.apps import AppConfig
from django.utils.module_loading import autodiscover_modules
class XadminConfig(AppConfig):
name = 'Xadmin'
def ready(self):
autodiscover_modules('Xadmin')
Django启动后会扫描每个app中Xadmin.py的文件,并执行
注册
#(在django注册时需要单例对象admin.site)
admin.site.register(models.Author)
- 在Xadmin.py中注册,自己写Xadmin源码,生成单例对象site(路径:Xadmin app中 service/Xadmin.py)
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 register(self, model, admin_class=None, **options):
if not admin_class:
admin_class = ModelXadmin
self._registry[model] = admin_class(model, self) # {Book:ModelAdmin(Book)}
site = XAdminSite()
- 在app中引Xadmin模块
- 先在app01中创建表,app02中创建表
app01/Xadmin.py
from Xadmin.service.Xadmin import site
from app01.models import *
# Register your models here.
site.register(Book)
site.register(Publish)
site.register(Author)
site.register(AuthorDetail)
app02/Xadmin.py
from Xadmin.service.Xadmin import site
from app02.models import *
# Register your models here.
site.register(Order)
设计URL
在项目中的urls.py中写url
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),
]
site中暂时没有urls方法,回到Xadmin app中service/Xadmin.py中添加urls方法
from django.conf.urls import url
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(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
def get_urls(self): # 一级路径 Book, Publish, Author, AuthorDetail
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), 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) # {Book:ModelAdmin(Book)}
site = XAdminSite()
可以登录Xadmin管理后台查看相关信息。
http://127.0.0.1:8000/Xadmin/app01/book/
http://127.0.0.1:8000/Xadmin/app01/book/add/
...其他路径
- 为了解决每个页面显示的内容与配置不一样,于是将二级分发转到ModelXadmin类中,以便显示models中不同的类的数据与对应的Config:
from django.conf.urls import url
from django.shortcuts import HttpResponse, render
class ModelXadmin(object):
def __init__(self, model, site):
self.model = model
self.site = site
def list_view(self, request):
print(self.model)
list_data = self.model.objects.all()
return render(request, "list_view.html", {"list_data": list_data})
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(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): # 一级路径 Book, Publish, Author, AuthorDetail
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), )
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)}
site = XAdminSite()