效果图:
Handler类里处理的增删改查、路由分发、给URL设置别名等包括以后还要添加的很多功能,每一个数据库的类都需要,所以我们要把Handler提取成一个基类。提取成基类后,每一个数据表都可以继承这个基类,然后有需要的话可以在这个基类的基础上重写函数,定制自己的方法。
Handler下新增函数get_urls是为了解耦Stark类下的get_urls
一、stark组件
stark/service/core_func.py
from django.urls import re_path from django.shortcuts import HttpResponse, render class StarkHandler(object): def __init__(self, model_class, prev=None): self.model_class = model_class self.prev = prev def list_view(self, request): """ 列表页面 :param request: :return: """ data_list = self.model_class.objects.all() return render(request, 'stark/data_list.html', {'data_list': data_list}) def add_view(self, request): """ 添加页面 :param request: :return: """ return HttpResponse('添加页面') def edit_view(self, request, pk): """ 编辑页面 :param request: :return: """ return HttpResponse('编辑页面') def delete_view(self, request, pk): """ 删除页面 :param request: :param pk: :return: """ return HttpResponse('删除页面') def get_urls(self): app_name, model_name = self.model_class._meta.app_label, self.model_class._meta.model_name if self.prev: patterns = [ re_path(r'^list/$', self.list_view, name="%s_%s_%s_list" % (app_name, model_name, self.prev)), re_path(r'^add/$', self.add_view, name="%s_%s_%s_add" % (app_name, model_name, self.prev)), re_path(r'^edit/(d+)/$', self.edit_view, name="%s_%s_%s_edit" % (app_name, model_name, self.prev)), re_path(r'^delete/(d+)/$', self.delete_view, name="%s_%s_%s_delete" % (app_name, model_name, self.prev)), ] else: patterns = [ re_path(r'^list/$', self.list_view, name="%s_%s_list" % (app_name, model_name,)), re_path(r'^add/$', self.add_view, name="%s_%s_add" % (app_name, model_name,)), re_path(r'^edit/(d+)/$', self.edit_view, name="%s_%s_edit" % (app_name, model_name,)), re_path(r'^delete/(d+)/$', self.delete_view, name="%s_%s_delete" % (app_name, model_name,)), ] patterns.extend(self.extra_urls()) return patterns def extra_urls(self): return [] class StarkSite(object): def __init__(self): self._registry = [] self.app_name = 'stark' self.namespace = 'stark' def register(self, model_class, handler_class=None, prev=None): """ :param model_class: 是models中的数据库表对应的类。 :param handler_class: 处理请求的视图函数所在的类 :param prev: 生成URL的前缀 :return: """ if not handler_class: handler_class = StarkHandler self._registry.append({'model_class': model_class, 'handler': handler_class(model_class, prev), 'prev': prev}) def get_urls(self): patterns = [] for item in self._registry: model_class = item['model_class'] handler = item['handler'] prev = item['prev'] app_name, model_name = model_class._meta.app_label, model_class._meta.model_name if prev: patterns.append( re_path(r'^%s/%s/%s/' % (app_name, model_name, prev,), (handler.get_urls(), None, None))) else: patterns.append(re_path(r'^%s/%s/' % (app_name, model_name,), (handler.get_urls(), None, None))) return patterns @property def urls(self): return self.get_urls(), self.app_name, self.namespace site = StarkSite()
二、业务处理
web/stark.py
from django.urls import re_path from django.shortcuts import HttpResponse from stark.service.core_func import site, StarkHandler from web import models class DepartmentHandler(StarkHandler): def extra_urls(self): """ 增加额外的URL :return: """ # 给部门增加了详情页:/stark/web/department/private/detail/1 return [ re_path(r'^detail/(d+)', self.detail_view) ] def detail_view(self, request, pk): return HttpResponse('详细页面') class UserInfoHandler(StarkHandler): def get_urls(self): """ 修改url :return: """ # 用户信息只有list和add的url patterns = [ re_path(r'^list/$', self.list_view), re_path(r'^add/$', self.add_view), ] return patterns site.register(models.Department, DepartmentHandler, prev='private') # 给部门的url增加了前缀:/stark/web/department/private/ site.register(models.UserInfo, UserInfoHandler)