码迷,mamicode.com
首页 > Web开发 > 详细

7 stark配置,生成2层 url

时间:2018-08-17 01:18:31      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:none   port   dmi   对象   turn   height   ready   settings   over   

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

标签:none   port   dmi   对象   turn   height   ready   settings   over   

原文地址:https://www.cnblogs.com/venicid/p/9490920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!