strak组件(3):URL别名的优化
将生成URL别名的功能进行解耦。效果和上一节的一样。
效果图:
新增函数
get_url_name(self, param) # 生成url别名,需要一个参数(list/add/edit/delete) get_list_url_name(self) # 获取列表页面URL的name property get_add_url_name(self) # 获取添加页面URL的name property get_edit_url_name(self) # 获取编辑页面URL的name property get_delete_url_name(self) # 获取删除页面URL的name property
Stark组件
from django.urls import re_path from django.shortcuts import HttpResponse, render class StarkHandler(object): def __init__(self, model_class, prev): 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_url_name(self, crud): app_label, model_name = self.model_class._meta.app_label, self.model_class._meta.model_name if self.prev: return '%s_%s_%s_%s' % (app_label, model_name, self.prev, crud) return '%s_%s_%s' % (app_label, model_name, crud) @property def get_list_url_name(self): """ 获取列表页面URL的name :return: """ return self.get_url_name('list') @property def get_add_url_name(self): """ 获取添加页面URL的name :return: """ return self.get_url_name('add') @property def get_edit_url_name(self): """ 获取编辑页面URL的name :return: """ return self.get_url_name('edit') @property def get_delete_url_name(self): """ 获取删除页面URL的name :return: """ return self.get_url_name('delete') def get_urls(self): patterns = [ re_path(r'^list/$', self.list_view, name=self.get_list_url_name), re_path(r'^add/$', self.add_view, name=self.get_add_url_name), re_path(r'^edit/(\d+)/$', self.edit_view, name=self.get_edit_url_name), re_path(r'^delete/(\d+)/$', self.delete_view, name=self.get_delete_url_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()