项目启动时自动生成URL
效果图:
知识点:
- Django启动前通过apps下的ready方法执行一个可以生成URL的py文件
- include函数主要返回有三个元素的一个元组。第一个是url配置(urlpatterns),第二个是app(app_name),第三个是命名空间(namespace)。
- 通过单例模式,我们反复操作一个对象。
项目名:stark_practice
业务app:web
stark app:stark
一、在业务表中生成数据库
web/models.py
from django.db import models class Department(models.Model): """ 部门表 """ title = models.CharField(verbose_name='部门名称', max_length=32) class UserInfo(models.Model): """ 用户表 """ name = models.CharField(verbose_name='姓名', max_length=32) age = models.CharField(verbose_name='年龄', max_length=32) email = models.CharField(verbose_name='邮箱', max_length=32) department = models.ForeignKey(verbose_name='部门', to='Department', on_delete=models.CASCADE)
二、加载django项目时自动执行stark文件
stark.apps.py
from django.apps import AppConfig from django.utils.module_loading import autodiscover_modules class StarkConfig(AppConfig): name = 'stark' def ready(self):
autodiscover_modules('stark') # 执行名为stark的py文件
三、stark组件
stark/service/core_function.py
from django.urls import re_path class StarkSite(object): def __init__(self): self._registry = [] self.app_name = 'stark' self.namespace = 'stark' def register(self, model_class, handler_class): """ :param model_class: 是models中的数据库表对应的类。 :param handler_class: 处理请求的视图函数所在的类 :return: """ """ self._registry = [ {'model_class': model.Department,'handler':DepartmentHandler(models.Department)}, {'model_class': model.UserInfo,'handler':UserInfo(models.UserInfo)}, {'model_class': model.Host,'handler':Host(models.Host)}, ] """ # 注意:handler_class接收的第一个参数是数据库表对应的类 self._registry.append({'model_class': model_class, 'handler': handler_class(model_class)}) def get_urls(self): patterns = [] for item in self._registry: model_class = item['model_class'] handler = item['handler'] app_name = model_class._meta.app_label # 获取当前类所在的app model_name = model_class._meta.model_name # 获取当前类所在的表名称 patterns.append(re_path(r'%s/%s/list' % (app_name, model_name), handler.list_view)) patterns.append(re_path(r'%s/%s/add' % (app_name, model_name), handler.add_view)) patterns.append(re_path(r'%s/%s/edit' % (app_name, model_name), handler.edit_view)) patterns.append(re_path(r'%s/%s/delete' % (app_name, model_name), handler.delete_view)) return patterns @property def urls(self): return self.get_urls(), self.app_name, self.namespace site = StarkSite()
四、使用strak组件
web/stark.py
from django.shortcuts import HttpResponse from stark.service.core_func import site from web import models class DepartmentHandler(object): def __init__(self, model_class): self.model_class = model_class def list_view(self, request): """ 列表页面 :param request: :return: """ return HttpResponse('列表页面') def add_view(self, request): """ 添加页面 :param request: :return: """ return HttpResponse('添加页面') def edit_view(self, request): """ 编辑页面 :param request: :return: """ return HttpResponse('编辑页面') def delete_view(self, request): """ 删除页面 :param request: :return: """ return HttpResponse('删除页面') class UserInfoHandler: def __init__(self, model_class): self.model_class = model_class def list_view(self, request): """ 列表页面 :param request: :return: """ return HttpResponse('列表页面') def add_view(self, request): """ 添加页面 :param request: :return: """ return HttpResponse('添加页面') def edit_view(self, request): """ 编辑页面 :param request: :return: """ return HttpResponse('编辑页面') def delete_view(self, request): """ 删除页面 :param request: :return: """ return HttpResponse('删除页面') site.register(models.Department, DepartmentHandler) site.register(models.UserInfo, UserInfoHandler)
五、注册URL
stark_practice/urls.py
from django.contrib import admin from django.urls import path, re_path from stark.service.core_func import site urlpatterns = [ path('admin/', admin.site.urls), re_path(r'^stark/', site.urls), ]