公用插件开发-Day1
定制启动执行文件
1.禁用Django自带的admin插件。
在settings文件中注释相关admin,auth,messages组件的注册。
INSTALLED_APPS = [ # 'django.contrib.admin', # 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', # 'django.contrib.messages', 'django.contrib.staticfiles', 'app01.apps.App01Config', 'app_plug.apps.AppPlugConfig', # 注册共用插件 ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', # 'django.contrib.auth.middleware.AuthenticationMiddleware', # 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'universal_plug.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')] , 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', # 'django.contrib.auth.context_processors.auth', # 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'universal_plug.wsgi.application' # Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Password validation # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ # { # 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', # }, # { # 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', # }, # { # 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', # }, # { # 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', # }, ] # Internationalization # https://docs.djangoproject.com/en/2.0/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_URL = '/static/'
2.启动文件的定制
首先需要在settings.py注册自己的插件application。
'app_plug.apps.AppPlugConfig', # 注册共用插件
我们需要的是在Django启动的准备状态的时候就运行我们自己的注册程序,将插件需要的信息获取到,所以我模仿了Django的admin的做法,在app_plug中放入apps.py写入启动规则:
class AppPlugConfig(AppConfig): name = 'app_plug' def ready(self): super(AppPlugConfig, self).ready() from django.utils.module_loading import autodiscover_modules autodiscover_modules('plug')
最后在需要插件的app中加入plug.py文件,在Django程序启动的时候就会先执行plug.py中的方法了。
GitHub:https://github.com/EditYJ/universal_plug/
posted on 2018-08-01 20:21 yujie158392613 阅读(167) 评论(0) 编辑 收藏 举报