Xadmin组件的url设计
Posted changwoo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xadmin组件的url设计相关的知识,希望对你有一定的参考价值。
1.注册一个新的app,创建Xadmin.py
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):
data_list = self.model.objects.all()
return render(request, ‘list_view.html‘, {‘data_list‘:data_list})
def add_view(self, request):
return HttpResponse(‘add_view‘)
def change_view(self, request, num):
return HttpResponse(‘change_view‘)
def delete_view(self, request, num):
return HttpResponse(‘delete_view‘)
def get_urls_operate(self):
temp = []
temp.append(url(r‘^$‘, self.list_view))
temp.append(url(r‘^add/$‘, self.add_view))
temp.append(url(r‘^(d+)/delete‘, self.delete_view))
temp.append(url(r‘^(d+)/change‘, self.change_view))
return temp
@property
def urls_operate(self):
return self.get_urls_operate(), None, None
class XadminSite(object):
def __init__(self):
self._registry = {}
def get_urls(self):
temp = []
for model, admin_class_obj in self._registry.items():
model_name = model._meta.model_name
app_name = model._meta.app_label
temp.append(url(r‘^{0}/{1}/‘.format(app_name, model_name), admin_class_obj.urls_operate))
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()
2.在另外的app下创建Xadmin.py
from Xadmin.service.Xadmin import site
from app02.models import *
site.register(demo)
site.register(demo02)
site.register(demo03)
site.register(Food)
print(‘-registry‘, site._registry)
print(‘--‘*20)
3.url.py
from Xadmin.service.Xadmin import site
urlpatterns = [
url(r‘^Xadmin/‘, site.urls),
]
以上是关于Xadmin组件的url设计的主要内容,如果未能解决你的问题,请参考以下文章
Xadmin添加用户小组件出错render() got an unexpected keyword argument 'renderer