day97

#admin.py

from django.contrib import admin
from . import models
from django.utils.safestring import mark_safe

# Register your models here.

#第一种方式
class LiuFeng(admin.ModelAdmin):
    list_display = ['name','email','xxxx']
    #定义展示的字段                    #定义自己的函数

    def xxxx(self,obj): #obj 为表对象(此处为UserInfo object)
        print("obj:",obj)
        tpl = "<a href='http://www.baidu.com/v={0}'>百度</a>".format(obj.id)
        return mark_safe(tpl)

admin.site.register(models.UserInfo,LiuFeng)
#第一个参数可以为一个列表,但需拥有相同的字段
#第二个参数不写默认使用ModelAdmin

#第二种方式
@admin.register(models.UserInfo)
class LiuFeng(admin.ModelAdmin):
    list_display = ['name','email','xxxx']
    #定义展示的字段                    #定义自己的函数

    def xxxx(self,obj): #obj 为表对象(此处为UserInfo object)
        print("obj:",obj)
        tpl = "<a href='http://www.baidu.com/v={0}'>百度</a>".format(obj.id)
        return mark_safe(tpl)
admin.py

 

# v1.py

from django.shortcuts import HttpResponse

class ModelNb(object):
    def __init__(self,model_class,site): #表 site对象
        self.model_class = model_class
        self.site = site

    def changelist_view(self,request):
        return HttpResponse('列表页面')

    def add_view(self,request):
        return HttpResponse('添加页面')

    def delete_view(self,request):
        return HttpResponse('删除页面')

    def change_view(self,request):
        return HttpResponse('修改页面')

    def get_urls(self):
        from django.conf.urls import url
        patterns = [
            url(r'^$',self.changelist_view),
            url(r'^add/$',self.add_view),
            url(r'^(.+)/delete/$',self.delete_view),
            url(r'^(.+)/change/$',self.change_view),
        ]

        return patterns

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




class NbSite(object):
    def __init__(self):
        self._register = {}
        self.name = 'nb'
        self.namespace = 'nb'


    def register(self,model,model_nb=None):
                      #表    #表配置
        if not model_nb:
            model_nb = ModelNb
        self._register[model] = model_nb(model,self)

    def login(self,request):
        return HttpResponse('登录页面')

    def logout(self,request):
        return HttpResponse('注销页面')

    def get_urls(self):
        patterns = []

        from django.conf.urls import url

        patterns += [
            url(r'^login/',self.login),
            url(r'^logout/',self.logout),
        ]

        for model_class,model_nb_obj in self._register.items():
            patterns += [
                url(r'^%s/%s/' %(model_class._meta.app_label,model_class._meta.model_name), model_nb_obj.urls),
            ]

        return patterns

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


site = NbSite()

# 1.实例化 self._register = {}
# 2. register方法
        #执行完了之后相当于把我注册的类以及他执行完后配置文件的实例化对象封装到了_register
# {
#     models.UserInfo: UserInfoAdmin(models.UserInfo,site对象)
#     models.UserGroup: UserInfoAdmin(models.UserInfo,site对象)
# }
v1.py

 

posted @ 2017-10-18 23:45  hsddon  阅读(180)  评论(0编辑  收藏  举报