1. 程式人生 > >django專案中settings常用配置

django專案中settings常用配置

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'n$(lkc$9r)%4!6#@
[email protected]
%o-tf7qv7#7kx8m1pdz+zt#+e4' # SECURITY WARNING: don't run with debug turned on in production! #debug模式,上線改成false DEBUG = True ALLOWED_HOSTS = ['*'] # Application definition #建立的APP放在下面 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'day01', 'day02', 'homework', 'day03', 'day04', 'day05', 'day06', 'day07', ] #中介軟體,可自定義 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', # 'middlewares.MyAOP.YjMiddleWare', ] ROOT_URLCONF = 'week01.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 = 'week01.wsgi.application' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.mysql', 'NAME': '資料庫名', 'PORT': 3306, 'PASSWORD': '密碼', 'USER': '賬號', 'HOST': 'IP地址', #可以通過下面設定,讓其隱藏 # 'PASSWORD':os.environ.get('DB_PWD'), # 'USER':os.environ.get('DB_USER'), }, } # Password validation # https://docs.djangoproject.com/en/1.11/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/1.11/topics/i18n/ #解析語言,可在admin介面看出區別,預設英文 LANGUAGE_CODE = 'zh-hans' #時區 TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' # 配置靜態檔案 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ] # 自定義使用者 # AUTH_USER_MODEL = 'day06.MyUser' # 自定義使用者認證 # AUTHENTICATION_BACKENDS = ( # 'day06.auth.MyBackend', # ) # 配置上傳檔案目錄 MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uploads') # 快取 redis CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } } #郵箱配置 EMAIL_USE_SSL = True EMAIL_HOST = 'smtp.qq.com' # 如果是 163 改成 smtp.163.com EMAIL_PORT = 465 EMAIL_HOST_USER = "郵箱" EMAIL_HOST_PASSWORD = "授權碼" DEFAULT_FROM_EMAIL = EMAIL_HOST_USER # VERIFY_CODE_MAX_AGE = 60 * 60 # 快取時間 VERIFY_CODE_MAX_AGE = None # 快取時間 # log檔案配置 ADMINS = ( ('wu', '接受log檔案郵箱'), ) EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' SERVER_EMAIL = EMAIL_HOST_USER LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': '%(asctime)s ' '[%(threadName)s:%(thread)d] ' '[%(name)s:%(lineno)d] ' '[%(module)s:%(funcName)s] [%(levelname)s]- %(message)s' }, 'easy': { 'format': '%%(asctime)s|%(funcName)s|%(message)s' } }, 'filters': { # 過濾條件 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', }, 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', } }, 'handlers': { # 'null': { # 'level': 'DEBUG', # 'class': 'logging.NullHandler', # }, 'mail_admins': { # 一旦線上程式碼報錯,郵件提示,要求debug是true 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', 'filters': ['require_debug_false'], }, 'debug': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', 'filename': os.path.join(BASE_DIR, 'log', 'debug.log'), # 檔案路徑 'maxBytes': 1024 * 1024 * 5, # 5M資料 'backupCount': 5, # 允許有幾個這樣的檔案 # 'formatter': 'standard', 'formatter': 'easy', }, 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'standard', }, }, 'loggers': { 'django': { 'handlers': ['console', 'debug'], 'level': 'DEBUG', 'propagate': False }, 'django.request': { 'handlers': ['debug', 'mail_admins'], 'level': 'ERROR', 'propagate': True, }, # 對於不在 ALLOWED_HOSTS 中的請求不傳送報錯郵件 'django.security.DisallowedHost': { 'handlers': ['debug'], 'propagate': False, }, } }