|
| 1 | +""" |
| 2 | +Django settings for application project. |
| 3 | +
|
| 4 | +Generated by 'django-admin startproject' using Django 3.2.3. |
| 5 | +
|
| 6 | +For more information on this file, see |
| 7 | +https://docs.djangoproject.com/en/3.2/topics/settings/ |
| 8 | +
|
| 9 | +For the full list of settings and their values, see |
| 10 | +https://docs.djangoproject.com/en/3.2/ref/settings/ |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +# Build paths inside the project like this: BASE_DIR / 'subdir'. |
| 17 | +BASE_DIR = Path(__file__).resolve().parent.parent |
| 18 | + |
| 19 | +# ================================================= # |
| 20 | +# ******************** 动态配置 ******************** # |
| 21 | +# ================================================= # |
| 22 | + |
| 23 | +from conf.env import * |
| 24 | + |
| 25 | +# Quick-start development settings - unsuitable for production |
| 26 | +# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ |
| 27 | + |
| 28 | +# SECURITY WARNING: keep the secret key used in production secret! |
| 29 | +SECRET_KEY = 'django-insecure--z8%exyzt7e_%i@1+#1mm=%lb5=^fx_57=1@a+_y7bg5-w%)sm' |
| 30 | + |
| 31 | +# SECURITY WARNING: don't run with debug turned on in production! |
| 32 | +DEBUG = True |
| 33 | + |
| 34 | +ALLOWED_HOSTS = ['*'] |
| 35 | + |
| 36 | +# Application definition |
| 37 | + |
| 38 | +INSTALLED_APPS = [ |
| 39 | + 'django.contrib.auth', |
| 40 | + 'django.contrib.contenttypes', |
| 41 | + 'django.contrib.sessions', |
| 42 | + 'django.contrib.messages', |
| 43 | + 'django.contrib.staticfiles', |
| 44 | + 'django_comment_migrate', |
| 45 | + 'rest_framework', |
| 46 | + 'django_filters', |
| 47 | + 'corsheaders', # 注册跨域app |
| 48 | + 'dvadmin.system', |
| 49 | + 'drf_yasg', |
| 50 | +] |
| 51 | + |
| 52 | +MIDDLEWARE = [ |
| 53 | + 'django.middleware.security.SecurityMiddleware', |
| 54 | + 'django.contrib.sessions.middleware.SessionMiddleware', |
| 55 | + 'corsheaders.middleware.CorsMiddleware', # 跨域中间件 |
| 56 | + 'django.middleware.common.CommonMiddleware', |
| 57 | + 'django.middleware.csrf.CsrfViewMiddleware', |
| 58 | + 'django.contrib.auth.middleware.AuthenticationMiddleware', |
| 59 | + 'django.contrib.messages.middleware.MessageMiddleware', |
| 60 | + 'django.middleware.clickjacking.XFrameOptionsMiddleware', |
| 61 | + 'dvadmin.utils.middleware.ApiLoggingMiddleware', |
| 62 | +] |
| 63 | + |
| 64 | +ROOT_URLCONF = 'application.urls' |
| 65 | + |
| 66 | +TEMPLATES = [ |
| 67 | + { |
| 68 | + 'BACKEND': 'django.template.backends.django.DjangoTemplates', |
| 69 | + 'DIRS': [os.path.join(BASE_DIR, 'templates')] |
| 70 | + , |
| 71 | + 'APP_DIRS': True, |
| 72 | + 'OPTIONS': { |
| 73 | + 'context_processors': [ |
| 74 | + 'django.template.context_processors.debug', |
| 75 | + 'django.template.context_processors.request', |
| 76 | + 'django.contrib.auth.context_processors.auth', |
| 77 | + 'django.contrib.messages.context_processors.messages', |
| 78 | + ], |
| 79 | + }, |
| 80 | + }, |
| 81 | +] |
| 82 | + |
| 83 | +WSGI_APPLICATION = 'application.wsgi.application' |
| 84 | + |
| 85 | +# Database |
| 86 | +# https://docs.djangoproject.com/en/3.2/ref/settings/#databases |
| 87 | + |
| 88 | +DATABASES = { |
| 89 | + 'default': { |
| 90 | + 'ENGINE': DATABASE_ENGINE, |
| 91 | + 'NAME': DATABASE_NAME, |
| 92 | + 'USER': DATABASE_USER, |
| 93 | + 'PASSWORD': DATABASE_PASSWORD, |
| 94 | + 'HOST': DATABASE_HOST, |
| 95 | + 'PORT': DATABASE_PORT, |
| 96 | + } |
| 97 | +} |
| 98 | +AUTH_USER_MODEL = 'system.Users' |
| 99 | +USERNAME_FIELD = 'username' |
| 100 | + |
| 101 | +# Password validation |
| 102 | +# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators |
| 103 | + |
| 104 | +AUTH_PASSWORD_VALIDATORS = [ |
| 105 | + { |
| 106 | + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', |
| 107 | + }, |
| 108 | + { |
| 109 | + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', |
| 110 | + }, |
| 111 | + { |
| 112 | + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', |
| 113 | + }, |
| 114 | + { |
| 115 | + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', |
| 116 | + }, |
| 117 | +] |
| 118 | + |
| 119 | +# Internationalization |
| 120 | +# https://docs.djangoproject.com/en/3.2/topics/i18n/ |
| 121 | + |
| 122 | +LANGUAGE_CODE = 'zh-hans' |
| 123 | + |
| 124 | +TIME_ZONE = 'Asia/Shanghai' |
| 125 | + |
| 126 | +USE_I18N = True |
| 127 | + |
| 128 | +USE_L10N = True |
| 129 | + |
| 130 | +USE_TZ = False |
| 131 | + |
| 132 | +# Static files (CSS, JavaScript, Images) |
| 133 | +# https://docs.djangoproject.com/en/3.2/howto/static-files/ |
| 134 | + |
| 135 | +STATIC_URL = '/static/' |
| 136 | +# # 设置django的静态文件目录 |
| 137 | +STATICFILES_DIRS = [ |
| 138 | + os.path.join(BASE_DIR, "static"), |
| 139 | +] |
| 140 | +# 收集静态文件,必须将 MEDIA_ROOT,STATICFILES_DIRS先注释 |
| 141 | +# python manage.py collectstatic |
| 142 | +# STATIC_ROOT=os.path.join(BASE_DIR,'static') |
| 143 | + |
| 144 | +# ================================================= # |
| 145 | +# ******************* 跨域的配置 ******************* # |
| 146 | +# ================================================= # |
| 147 | + |
| 148 | +# 全部允许配置 |
| 149 | +CORS_ORIGIN_ALLOW_ALL = True |
| 150 | +# 允许cookie |
| 151 | +CORS_ALLOW_CREDENTIALS = True # 指明在跨域访问中,后端是否支持对cookie的操作 |
| 152 | + |
| 153 | +# ================================================= # |
| 154 | +# ********************* 日志配置 ******************* # |
| 155 | +# ================================================= # |
| 156 | + |
| 157 | +# log 配置部分BEGIN # |
| 158 | +SERVER_LOGS_FILE = os.path.join(BASE_DIR, 'logs', 'server.log') |
| 159 | +ERROR_LOGS_FILE = os.path.join(BASE_DIR, 'logs', 'error.log') |
| 160 | +if not os.path.exists(os.path.join(BASE_DIR, 'logs')): |
| 161 | + os.makedirs(os.path.join(BASE_DIR, 'logs')) |
| 162 | + |
| 163 | +# 格式:[2020-04-22 23:33:01][micoservice.apps.ready():16] [INFO] 这是一条日志: |
| 164 | +# 格式:[日期][模块.函数名称():行号] [级别] 信息 |
| 165 | +STANDARD_LOG_FORMAT = '[%(asctime)s][%(name)s.%(funcName)s():%(lineno)d] [%(levelname)s] %(message)s' |
| 166 | +CONSOLE_LOG_FORMAT = '[%(asctime)s][%(name)s.%(funcName)s():%(lineno)d] [%(levelname)s] %(message)s' |
| 167 | + |
| 168 | +LOGGING = { |
| 169 | + 'version': 1, |
| 170 | + 'disable_existing_loggers': False, |
| 171 | + 'formatters': { |
| 172 | + 'standard': { |
| 173 | + 'format': STANDARD_LOG_FORMAT |
| 174 | + }, |
| 175 | + 'console': { |
| 176 | + 'format': CONSOLE_LOG_FORMAT, |
| 177 | + 'datefmt': '%Y-%m-%d %H:%M:%S', |
| 178 | + }, |
| 179 | + 'file': { |
| 180 | + 'format': CONSOLE_LOG_FORMAT, |
| 181 | + 'datefmt': '%Y-%m-%d %H:%M:%S', |
| 182 | + }, |
| 183 | + }, |
| 184 | + 'handlers': { |
| 185 | + 'file': { |
| 186 | + 'level': 'INFO', |
| 187 | + 'class': 'logging.handlers.RotatingFileHandler', |
| 188 | + 'filename': SERVER_LOGS_FILE, |
| 189 | + 'maxBytes': 1024 * 1024 * 100, # 100 MB |
| 190 | + 'backupCount': 5, # 最多备份5个 |
| 191 | + 'formatter': 'standard', |
| 192 | + 'encoding': 'utf-8', |
| 193 | + }, |
| 194 | + 'error': { |
| 195 | + 'level': 'ERROR', |
| 196 | + 'class': 'logging.handlers.RotatingFileHandler', |
| 197 | + 'filename': ERROR_LOGS_FILE, |
| 198 | + 'maxBytes': 1024 * 1024 * 100, # 100 MB |
| 199 | + 'backupCount': 3, # 最多备份3个 |
| 200 | + 'formatter': 'standard', |
| 201 | + 'encoding': 'utf-8', |
| 202 | + }, |
| 203 | + 'console': { |
| 204 | + 'level': 'INFO', |
| 205 | + 'class': 'logging.StreamHandler', |
| 206 | + 'formatter': 'console', |
| 207 | + } |
| 208 | + }, |
| 209 | + 'loggers': { |
| 210 | + # default日志 |
| 211 | + '': { |
| 212 | + 'handlers': ['console', 'error', 'file'], |
| 213 | + 'level': 'INFO', |
| 214 | + }, |
| 215 | + # 数据库相关日志 |
| 216 | + 'django.db.backends': { |
| 217 | + 'handlers': [], |
| 218 | + 'propagate': True, |
| 219 | + 'level': 'INFO', |
| 220 | + }, |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +# ================================================= # |
| 225 | +# *************** REST_FRAMEWORK配置 *************** # |
| 226 | +# ================================================= # |
| 227 | + |
| 228 | +REST_FRAMEWORK = { |
| 229 | + 'DATETIME_FORMAT': "%Y-%m-%d %H:%M:%S", # 日期时间格式配置 |
| 230 | + 'DATE_FORMAT': "%Y-%m-%d", |
| 231 | + 'DEFAULT_FILTER_BACKENDS': ( |
| 232 | + 'django_filters.rest_framework.DjangoFilterBackend', |
| 233 | + 'rest_framework.filters.SearchFilter', |
| 234 | + 'rest_framework.filters.OrderingFilter', |
| 235 | + |
| 236 | + ), |
| 237 | + 'DEFAULT_PAGINATION_CLASS': 'dvadmin.utils.pagination.CustomPagination', # 自定义分页 |
| 238 | + 'DEFAULT_AUTHENTICATION_CLASSES': ( |
| 239 | + 'rest_framework_simplejwt.authentication.JWTAuthentication', |
| 240 | + 'rest_framework.authentication.SessionAuthentication', |
| 241 | + ), |
| 242 | + 'EXCEPTION_HANDLER': 'dvadmin.utils.exception.CustomExceptionHandler', # 自定义的异常处理 |
| 243 | +} |
| 244 | + |
| 245 | +# ================================================= # |
| 246 | +# ****************** simplejwt配置 ***************** # |
| 247 | +# ================================================= # |
| 248 | +from datetime import timedelta |
| 249 | + |
| 250 | +SIMPLE_JWT = { |
| 251 | + # token有效时长 |
| 252 | + 'ACCESS_TOKEN_LIFETIME': timedelta(hours=2), |
| 253 | + # token刷新后的有效时间 |
| 254 | + 'REFRESH_TOKEN_LIFETIME': timedelta(days=1), |
| 255 | + # 设置前缀 |
| 256 | + 'AUTH_HEADER_TYPES': ('JWT',), |
| 257 | +} |
| 258 | + |
| 259 | +# ================================================= # |
| 260 | +# ******************** 其他配置 ******************** # |
| 261 | +# ================================================= # |
| 262 | +API_LOG_ENABLE = True |
| 263 | +# API_LOG_METHODS = 'ALL' # ['POST', 'DELETE'] |
| 264 | +API_LOG_METHODS = ['POST', 'UPDATE', 'DELETE'] # ['POST', 'DELETE'] |
| 265 | +DJANGO_CELERY_BEAT_TZ_AWARE = False |
| 266 | +CELERY_TIMEZONE = 'Asia/Shanghai' # celery 时区问题 |
0 commit comments