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#@p!1@fbtv%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,
        },
    }
}

猜你喜欢

转载自blog.csdn.net/wujialaoer/article/details/83588182
今日推荐