一、admin组件使用
Django本身提供了基于 web 的管理工具。其管理工具是django.contrib的一部分,可在settings.py中的 INSTALLED_APPS 看到:
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, "app01" ]
- 在使用admin界面管理数据之前,需要先注册一个超级用户,可在终端通过python manage.py createsuperuser来创建。
二、源码分析
1、在启动Django时,会自动执行初始化文件里的autodiscover函数,从而循环加载所有已经注册的app中的admin.py文件
def autodiscover(): autodiscover_modules(‘admin‘, register_to=site)
2、执行代码
# 这里为自定制的显示样式 class BookAdmin(admin.ModelAdmin): list_display = ("title",‘publishDate‘, ‘price‘) admin.site.register(Book, BookAdmin) # model类名,样式类名(不写使用默认样式) admin.site.register(Publish)
3、admin.site会实例化一个对象
- 这里应用的是单例模式,每一个app中的每一个admin.site实例化的对象都是同一个
4、执行register方法
def register(self, model_or_iterable, admin_class=None, **options): # model_or_iterable表示扫描到的每一个已注册的model对象 # admin_class传一个类,这个类里面定义的是admin页面的显示样式 if not admin_class: admin_class = ModelAdmin # ModelAdmin是默认的显示样式 if model in self._registry: # 判断每个model对象是否在_registry字典里 raise AlreadyRegistered(‘The model %s is already registered‘ % model.__name__) self._registry[model] = admin_class(model, self) # 以model对象为键,指定的样式类为值存放到_registry里 # 以上无用部分已忽略
到此,完成全部注册操作
5、admin的URL配置
# 在某一py文件下写下如下代码from django.contrib import adminfrom .models import *
urlpatterns = [ url(r‘^admin/‘, admin.site.urls), #调用AdminSite类下的urls方法。 ]
class AdminSite(object): def get_urls(self): from django.conf.urls import url, include from django.contrib.contenttypes import views as contenttype_views # 这一装饰器可以在调用视图函数的时候将request传给该函数 def wrap(view, cacheable=False): def wrapper(*args, **kwargs): return self.admin_view(view, cacheable)(*args, **kwargs) # admin_view代码已省略 return update_wrapper(wrapper, view) # Admin-site-wide views. urlpatterns = [ url(r‘^$‘, wrap(self.index), name=‘index‘), url(r‘^login/$‘, self.login, name=‘login‘), url(r‘^logout/$‘, wrap(self.logout), name=‘logout‘), ] # Add in each model‘s views, and create a list of valid URLS for the # app_index valid_app_labels = [] for model, model_admin in six.iteritems(self._registry): urlpatterns += [ url(r‘^%s/%s/‘ % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)), ] if model._meta.app_label not in valid_app_labels: valid_app_labels.append(model._meta.app_label) # If there were ModelAdmins registered, we should have a list of app # labels for which we need to allow access to the app_index view, if valid_app_labels: regex = r‘^(?P<app_label>‘ + ‘|‘.join(valid_app_labels) + ‘)/$‘ urlpatterns += [ url(regex, wrap(self.app_index), name=‘app_list‘), ] return urlpatterns @property def urls(self): return self.get_urls(), ‘admin‘, self.name
原文地址:https://www.cnblogs.com/value-code/p/8353304.html
时间: 2024-11-02 14:29:55