7 stark配置,生成2层 url
Posted venicid
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7 stark配置,生成2层 url相关的知识,希望对你有一定的参考价值。
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)
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()
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()
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的主要内容,如果未能解决你的问题,请参考以下文章