1. 程式人生 > >7 stark配置,生成2層 url

7 stark配置,生成2層 url

desc setting modules bee 用戶 dac hang 組件開發 conf

1、stark組件開發

stark組件開發,仿django自帶得admin組件。

1、django的admin配置

技術分享圖片技術分享圖片

技術分享圖片model 技術分享圖片
from django.contrib import admin

# Register your models here.
from .models import *

class UserAdmin(admin.ModelAdmin):
    list_display = ["pk",name,age]
    list_filter = [name,age]

    # 定制action具體方法
def func(self,request,queryset): queryset.update(age=44) func.short_description = "批量初始化操作" actions = [func] admin.site.register(UserInfo,UserAdmin) admin.site.register(Book)
admin.py
makemigrations
migrate
createsuperuser (alex 1234qwer)

技術分享圖片

2、starkadmin註冊register

1. startapp stark

技術分享圖片

stark/service/stark.py (單例)

技術分享圖片
class ModelStark(object):
    """Book,Userinfo對象"""
    def __init__(self, model, site):
        self.model = model
        self.site = site


class StarkSite(object):
    """site單例對象的創建類"""
    def __init__(self):
        self._registry 
= {} def register(self,model, stark_class=None): if not stark_class: stark_class = ModelStark self._registry[model] = stark_class(model,self) site = StarkSite()
View Code


2. settings中配置

技術分享圖片

技術分享圖片

from django.apps import AppConfig
from django.utils.module_loading import autodiscover_modules

class App01Config(AppConfig):
    name = app01

    # 程序啟動時,掃描app下得指定文件(starkadmin.py)並執行
    def ready(self):
        autodiscover_modules(starkadmin)

3、創建文件starkadmin:仿admin文件

app01/starkadmin.py

技術分享圖片

4、url

技術分享圖片

技術分享圖片

2、生成url

1、2層url分發初級版本

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片
from django.conf.urls import url
from django.shortcuts import HttpResponse


class ModelStark(object):
    """Book,Userinfo對象"""
    def __init__(self, model, site):
        self.model = model
        self.site = site


class StarkSite(object):
    """site單例對象的創建類"""
    def __init__(self):
        self._registry = {}

    def register(self,model, stark_class=None):
        if not stark_class:
            stark_class = ModelStark

        self._registry[model] = stark_class(model,self)

    def list_view(self,request):
        return HttpResponse(list_view)

    def add(self,request):
        return HttpResponse(add)

    def delete(self, request, id):
        return HttpResponse(delete)

    def change(self, request, id):
        return HttpResponse(change)

    def get_urls2(self):
        """構造 add/delete/change"""
        temp = []
        temp.append(url(r^$, self.list_view))
        temp.append(url(r^add/, self.add))
        temp.append(url(r^(\d+)/delete/, self.delete))
        temp.append(url(r^(\d+)/change/, self.change))

        return temp

    @property
    def urls2(self):
        return self.get_urls2(), None, None

    def get_urls(self):
        """構造urls:app01/book"""
        temp = []

        print(self._registry)
        for model, stark_class_obj in self._registry.items():
            model_name = model._meta.model_name
            app_label = model._meta.app_label

            # 分發增刪改查
            temp.append(url(r^%s/%s/ % (app_label, model_name), self.urls2))

        return temp

    @property
    def urls(self):
        return self.get_urls(), None, None

site = StarkSite()
View Code

技術分享圖片技術分享圖片

技術分享圖片技術分享圖片

2、如何保證不同的Model模型表對應不同的頁面展示

技術分享圖片技術分享圖片

技術分享圖片

技術分享圖片

3、ModelStark

ModelStark()
self.model:用戶訪問的模型表

  技術分享圖片

技術分享圖片

4、二次分發優化版本

from django.conf.urls import url
from django.shortcuts import HttpResponse


class ModelStark(object):
    """Book,Userinfo對象"""
    def __init__(self, model, site):
        self.model = model
        self.site = site

    def list_view(self, request):
        print(self.model)   # <class ‘app01.models.Book‘>  用戶訪問的模型表
        return HttpResponse(list_view)

    def add(self, request):
        return HttpResponse(add)

    def delete(self, request, id):
        return HttpResponse(delete)

    def change(self, request, id):
        return HttpResponse(change)

    def get_urls2(self):
        """構造 add/delete/change"""
        temp = []
        temp.append(url(r^$, self.list_view))
        temp.append(url(r^add/, self.add))
        temp.append(url(r^(\d+)/delete/, self.delete))
        temp.append(url(r^(\d+)/change/, self.change))

        return temp

    @property
    def urls2(self):
        return self.get_urls2(), None, None

class StarkSite(object):
    """site單例對象的創建類"""
    def __init__(self):
        self._registry = {}

    def register(self,model, stark_class=None):
        if not stark_class:
            stark_class = ModelStark

        self._registry[model] = stark_class(model,self)

    def get_urls(self):
        """構造urls:app01/book  app01/Userinfo"""
        temp = []

        print(self._registry)
        for model, stark_class_obj in self._registry.items():
            model_name = model._meta.model_name
            app_label = model._meta.app_label

            print(model,---->,stark_class_obj)
            """
            <class ‘app01.models.UserInfo‘> ----> <app01.starkadmin.UserConfig object at 0x00000072DDB65198>
            <class ‘app01.models.Book‘> ----> <stark.service.stark.ModelStark object at 0x00000072DDB65240>
            """
            # 分發增刪改查
            temp.append(url(r^%s/%s/ % (app_label, model_name), stark_class_obj.urls2))
            """
            path(‘app01/userinfo/‘,UserConfig(Userinfo,site).urls2),
            path(‘app01/book/‘,ModelStark(Book,site).urls2),
            """
        return temp

    @property
    def urls(self):
        return self.get_urls(), None, None

site = StarkSite()

7 stark配置,生成2層 url