zoukankan      html  css  js  c++  java
  • Django中admin

    1. admin

    1. url的使用

         情况1:

    url(r'^index/',([
     url(r'^test01/',([
       url(r'^test04/',test04),
       url(r'^test05/',test05),
          ],None,None)),
     url(r'^test02/',test02),
     url(r'^test03/',test03),
      ],None,None))

    情况2:

    url(r'^login/',views.login)


    2. 单例模式

    定义:一个类只允许实例一个对象
    目的:为了数据统一
    比如,某个服务器程序的配置信息存放在一个文件中,客户端通过一个 AppConfig 的类来读取配置文件的信息。
    如果在程序运行期间,有很多地方都需要使用配置文件的内容,也就是说,很多地方都需要创建 AppConfig 对象的实例,
    这就导致系统中存在多个 AppConfig 的实例对象,而这样会严重浪费内存资源,尤其是在配置文件内容很多的情况下。
    事实上,类似 AppConfig 这样的类,我们希望在程序运行期间只存在一个实例对象。

    在 Python 中,我们可以用多种方法来实现单例模式:
    1. 使用模块
        Python 的模块就是天然的单例模式,因为模块在第一次导入时,会生成 .pyc 文件(中间文件),
        当第二次导入时,就会直接加载 .pyc 文件,而不会再次执行模块代码。
        因此,我们只需把相关的函数和数据定义在一个模块中,就可以获得一个单例对象了。

    # mysingleton.py
    class My_Singleton(object):
        x=12
        def foo(self):
            print(self.x)
     
    my_singleton = My_Singleton()
    
    #main.py
    from mysingleton import my_singleton,My_Singleton
    a=My_Singleton()
    b=My_Singleton()
    print(id(a))
    print(id(b))
    #输出结果不一样,得从模块里拿实例对象,才是单例模式
    print(id(my_singleton))
    from mysingleton import my_singleton
    print(id(my_singleton))
    #这里my_singleton的id是一样的


    2. 使用 __new__

    class Singleton(object):
        _instance = None
        def __new__(cls, *args, **kw):
            if not cls._instance:
                cls._instance = super(Singleton, cls).__new__(cls, *args, **kw)
            return cls._instance
    
    class MyClass(Singleton):
        a = 1
    one = MyClass()
    two = MyClass()
    print(one == two) #True
    print(one is two) #True
    print(id(one), id(two))    #140229669392512 140229669392512        

    3. 使用装饰器(decorator)
    4. 使用元类(metaclass)

    3. admin源码

    1. 注册

        --admin.py

        admin.site.register(UserInfo)

        admin.site是一个单例对象

        admin.site._registry是字典形式,所有注册的models都在这里

        def register(self, model_or_iterable, admin_class=None, **options):
            """
            Registers the given model(s) with the given admin class.
    
            The model(s) should be Model classes, not instances.
    
            If an admin class isn't given, it will use ModelAdmin (the default
            admin options). If keyword arguments are given -- e.g., list_display --
            they'll be applied as options to the admin class.
    
            If a model is already registered, this will raise AlreadyRegistered.
    
            If a model is abstract, this will raise ImproperlyConfigured.
            """
            if not admin_class:
                admin_class = ModelAdmin
    
            if isinstance(model_or_iterable, ModelBase):
                model_or_iterable = [model_or_iterable]
            for model in model_or_iterable:
                if model._meta.abstract:
                    raise ImproperlyConfigured(
                        'The model %s is abstract, so it cannot be registered with admin.' % model.__name__
                    )
    
                if model in self._registry:
                    raise AlreadyRegistered('The model %s is already registered' % model.__name__)
    
                # Ignore the registration if the model has been
                # swapped out.
                if not model._meta.swapped:
                    # If we got **options then dynamically construct a subclass of
                    # admin_class with those **options.
                    if options:
                        # For reasons I don't quite understand, without a __module__
                        # the created class appears to "live" in the wrong place,
                        # which causes issues later on.
                        options['__module__'] = __name__
                        admin_class = type("%sAdmin" % model.__name__, (admin_class,), options)
    
                    # Instantiate the admin class to save in the registry
                    self._registry[model] = admin_class(model, self)

    2. 设计url    

    def get_urls():
        temp=[]
        print("_registry",admin.site._registry)
        for model.admin in admin.site._registry.items():
            #print("model",model)#所有的注册模型表
            #print(model._meta.model_name) #模型的名称
            #print(model._meta.app_label) #对应APP的名称
            model_name=model._meta.model_name
            app_label=model._meta.app_label
            temp.append(url(r"%s/%s/"%(app_label,model_name),login))
        return temp
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^login/',(get_urls(),None,None)),
    ]
  • 相关阅读:
    mac 终端常见指令
    git常见指令
    iOS8的autolayout和size class
    UIWindow详解
    操作系统Unix、Windows、Mac OS、Linux的故事
    iOS引用当前显示的UIAlertView
    Unexpected CFBundleExecutable Key
    《CODE》讲了什么?
    exit和return的区别
    php 登录注册api接口代码
  • 原文地址:https://www.cnblogs.com/yangyuqing/p/9987145.html
Copyright © 2011-2022 走看看