• stark组件(4):列表定义列,展示数据库数据


    效果图:

    一、Stark组件

    stark/service/core_func.py

    from django.urls import re_path
    from django.shortcuts import HttpResponse, render
    
    
    class StarkHandler(object):
        list_display = []
    
        def __init__(self, model_class, prev):
            self.model_class = model_class
            self.prev = prev
    
        def list_view(self, request):
            """
            列表页面
            :param request:
            :return:
            """
    
            # 1. 处理表格的表头
            header_list = []
            for field in self.list_display:
                verbose_name = self.model_class._meta.get_field(field).verbose_name
                header_list.append(verbose_name)
    
            # 2. 处理表的内容
            data_list = self.model_class.objects.all()
            print(data_list)
            body_list = []
            print(body_list)
            for obj in data_list:
                tr_list = []
                for field in self.list_display:
                    tr_list.append(getattr(obj, field))  # 获取字段的值
                body_list.append(tr_list)
    
            context = {
                'data_list': data_list,
                'header_list': header_list,
                'body_list': body_list,
            }
    
            return render(request, 'stark/data_list.html', context)
    
        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()

    二、业务处理

    web/stark.py

    from stark.service.core_func import site, StarkHandler
    
    from web import models
    
    
    class DepartmentHandler(StarkHandler):
        list_display = ['title']
    
    
    class UserInfoHandler(StarkHandler):
        list_display = ['name', 'age', 'email']
    
    
    site.register(models.Department, DepartmentHandler) 
    site.register(models.UserInfo, UserInfoHandler)

     

  • 相关阅读:
    CentOS 6.3下Samba服务器的安装与配置(转)
    利用香蕉派自制电视盒子
    利用arduino制作瓦力万年历-1.0
    arduino:int & double 转string 适合12864下使用
    centos 6.X下建立arduino开发环境
    树莓派学习笔记(7):利用bypy实现树莓派NAS同步百度云
    直接插入排序
    直接选择排序
    快速排序算法
    git 分支管理 推送本地分支到远程分支等
  • 原文地址:https://www.cnblogs.com/lshedward/p/10565382.html
Copyright © 2020-2023  润新知