zoukankan      html  css  js  c++  java
  • 10.15仿admin开发stark组件(一)

    2018-10-15 12:28:50

    越努力,越幸运!永远不要高估自己!

    低调做人,高调做事!

    明天开stark项目!!

    admin 参考连接: http://www.cnblogs.com/yuanchenqi/articles/8323452.html

    admin里面的一些配置

    admin.py

    from django.contrib import admin
    
    
    from .models import *
    
    # 带*的是必回的,其他了解,重点的注意 list_filter
    class BookConfig(admin.ModelAdmin):
        list_display=["id","user","room","date","time_id"] # 不能放多对多字段   *
        list_display_links=["user"] # *
        list_filter=["user","room","date"]  #   *
        list_editable=["room"]  # 不能和list_display_links 重复
    
        #date_hierarchy = "date"
        #fields=["user","room"]
        #exclude=["user","room"]
    
        ordering=["date","time_id"]     # 排序
    
        # 定制Action行为具体方法    *
        def func(self, request, queryset):
    
            print(self, request, queryset)
            queryset.update(date="2012-12-12")
    
        func.short_description = "批量初始化操作"
        actions = [func, ]
    
    class UserConfig(admin.ModelAdmin):
        list_display = ["username","tel"]
        list_filter = ["gf","username"]
        search_fields=["username","tel"]
    
    
    admin.site.register(Book,BookConfig)
    
    #print(admin.site._registry)
    
    admin.site.register(UserInfo,UserConfig)
    admin.site.register(Room)
    admin.site.register(GF)

    仿照admin的url注册:

    就是一级分发到二级分发,然后返回一个([], None, None)

    贴上源码

    urls.py

    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    from django.shortcuts import  HttpResponse
    
    def yuan(request):
    
        return HttpResponse("Yuan")
    
    def test01(request):
    
        return HttpResponse("test01")
    
    def test02(request):
    
        return HttpResponse("test02")
    
    def test03(request):
    
        return HttpResponse("test03")
    
    
    def test04(request):
    
        return HttpResponse("test04")
    
    def test05(request):
    
        return HttpResponse("test05")
    
    
    
    def add(request):
        return HttpResponse("add")
    def delete(request,id):
        return HttpResponse("delete")
    def change(request,id):
        return HttpResponse("change")
    def list_view(request):
        return HttpResponse("list_view")
    
    
    def get_urls2():
    
        temp=[]
        temp.append(url(r"^add/",add))
        temp.append(url(r"^(d+)/delete/",delete))
        temp.append(url(r"^(d+)/change/",change))
        temp.append(url(r"^$",list_view))
        return temp
    
    def get_urls():
    
        temp=[]
        print("_registry",admin.site._registry)
    
        for model,admin_class_obj in admin.site._registry.items():
             print("model",model) # 所有的注册模型表
    
             # < class 'app01.models.Book'>----->     "book"  "app01"
             # < class 'app01.models.Room'>----->     "room"  "app01"
             # print("===>",model._meta.model_name)
             # print("===>",model._meta.app_label)
             model_name=model._meta.model_name
             app_label=model._meta.app_label
             temp.append(url(r"%s/%s/"%(app_label,model_name),(get_urls2(),None,None)))
    
    
        return temp
    
    
    
    
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^index/', views.index),
        url(r'^login/', views.login),
        url(r'^book/', views.book),
    
    
        url(r"^yuan/",(get_urls(),None,None))
    
    
    ]

    笔记

    day87
    
    权限
    会议室预定
    admin与stark
    
    复习面向对象知识
    
    crm
    
    admin
    
    使用
    
    知识点1:
    
        url()的使用
    
        情况1:url(r'^book/', views.book),  # book(request)
    
        情况2 分发:
        url(r"^yuan/", ([
                            url(r'^test01/', ([
                                                  url(r'^test04/', test04),
                                                  url(r'^test05/', test05),
                                              ], None, None)),
                            url(r'^test02/', test02),
                            url(r'^test03/', test03),
                        ], None, None)
            )
    
    知识点2:
    
        单例模式
            生成单例模式的方式:
            (1)使用 __new__2)使用模块
                 class A()
                    pass
                 a=A()
    
    admin源码:
    
    1 注册 admin.py
    
        admin.site.register(Book,BookConfig)
    
        源码:
    
            class AdminSite():
                 def __init__(self, name='admin'):
                     self._registry = {}
    
                 def register(self,model,admin_class):
                     if not admin_class:
                          admin_class = ModelAdmin
    
                     self._registry[model] = admin_class(model, self)
    
    
    
            site=AdminSite()
    
    
    
    2 设计url
    
        如何通过model类变量获取该模型的字符串名称和该模型所在app的字符串名称:
        print("===>", model._meta.model_name)
        print("===>", model._meta.app_label)
    ###################################################################
    
    
    
    def add(request):
        return HttpResponse("add")
    def delete(request,id):
        return HttpResponse("delete")
    def change(request,id):
        return HttpResponse("change")
    def list_view(request):
        return HttpResponse("list_view")
    
    
    def get_urls2():
    
        temp=[]
        temp.append(url(r"^add/",add))
        temp.append(url(r"^(d+)/delete/",delete))
        temp.append(url(r"^(d+)/change/",change))
        temp.append(url(r"^$",list_view))
        return temp
    
    def get_urls():
    
    
        temp=[]
        print("_registry",admin.site._registry)
    
        for model,admin_class_obj in admin.site._registry.items():
             print("model",model) # 所有的注册模型表
    
             # < class 'app01.models.Book'>----->     "book"  "app01"
             # < class 'app01.models.Room'>----->     "room"  "app01"
             # print("===>",model._meta.model_name)
             # print("===>",model._meta.app_label)
    
             model_name=model._meta.model_name
             app_label=model._meta.app_label
             temp.append(url(r"%s/%s/"%(app_label,model_name),(get_urls2(),None,None)))
    
        return temp
    
    
    
    
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^index/', views.index),
        url(r'^login/', views.login),
        url(r'^book/', views.book),
    
    
        url(r"^yuan/",(get_urls(),None,None))
    
    
    ]
    
    
    
    
    
    
    
    
    
    restful
    vue
    路飞学城
  • 相关阅读:
    [Leetcode] Median of Two Sorted Arrays
    [Jobdu] 题目1463:招聘会
    [Leetcode] Merge Two Sorted Lists
    [Leetcode] Combinations
    [Leetcode] Populating Next Right Pointers in Each Node II
    [Leetcode] Insertion Sort List
    redis在Web中的使用
    设计模式:单例模式
    设计模式:基本法则
    设计模式:工厂模式
  • 原文地址:https://www.cnblogs.com/zhen1996/p/9790376.html
Copyright © 2011-2022 走看看