"""
Django settings for week01 project.
Generated by 'django-admin startproject' using Django 1.11.16.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
from datetime import timedelta
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 = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djcelery',
'app名',
]
#自己安装;详见博主其他博客
import djcelery
djcelery.setup_loader()
BROKER_URL = "redis://localhost:6379/3" # 设置消息队列的url
CELERY_CONCURRENCY = 2 # 指定worker的数量
CELERY_RESULT_BACKEND = 'redis://localhost:6379/4' # 设置结果存放
from datetime import timedelta
from celery.schedules import crontab
#设置时间
CELERYBEAT_SCHEDULE = {
'every-3-seconds-run-write_task': {
'task': 'day09.tasks.write_task', # 指定要执行的函数
'schedule': crontab(minute=3, hour=4, day_of_week='2,4'), # timedelta(seconds=3), # 计划时间
'args': () # 参数
}
}
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 = '工程名.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 = '工程.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 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/
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 = '自定义时app.MyUser'
# 自定义用户认证
# AUTHENTICATION_BACKENDS = (
# 'appming.auth.MyBackend',
# )
# 配置上传文件目录
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uploads')
# 缓存
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,
},
}
}
django项目中settings常用配置
最新推荐文章于 2023-03-21 23:06:00 发布