273 lines
8.4 KiB
Python
273 lines
8.4 KiB
Python
"""
|
||
Django settings for mysite project.
|
||
|
||
Generated by 'django-admin startproject' using Django 5.1.2.
|
||
|
||
For more information on this file, see
|
||
https://docs.djangoproject.com/en/5.1/topics/settings/
|
||
|
||
For the full list of settings and their values, see
|
||
https://docs.djangoproject.com/en/5.1/ref/settings/
|
||
"""
|
||
|
||
import os
|
||
from pathlib import Path
|
||
|
||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||
|
||
# Quick-start development settings - unsuitable for production
|
||
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
|
||
|
||
# SECURITY WARNING: keep the secret key used in production secret!
|
||
SECRET_KEY = "django-insecure-$c@01csjiwemsyx_%v@h5l#1a6k&y#23is6kpib34p7d0esl@n"
|
||
|
||
# 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",
|
||
"rest_framework",
|
||
"django_filters",
|
||
"apps.area",
|
||
"apps.dictionary",
|
||
"apps.menu",
|
||
"apps.menu_button",
|
||
"apps.role",
|
||
"apps.system_user",
|
||
]
|
||
|
||
MIDDLEWARE = [
|
||
"utils.exception.CustomExceptionMiddleware", # 异常报错中间件
|
||
"django.middleware.security.SecurityMiddleware",
|
||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||
"django.middleware.common.CommonMiddleware",
|
||
"django.middleware.locale.LocaleMiddleware", # 添加 LocaleMiddleware
|
||
# 'django.middleware.csrf.CsrfViewMiddleware',
|
||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||
"django.contrib.messages.middleware.MessageMiddleware",
|
||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||
]
|
||
|
||
ROOT_URLCONF = "mysite.urls"
|
||
|
||
TEMPLATES = [
|
||
{
|
||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||
"DIRS": [],
|
||
"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 = "mysite.wsgi.application"
|
||
|
||
# Database
|
||
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
|
||
|
||
DATABASES = {
|
||
"default": {
|
||
"ENGINE": "django.db.backends.mysql", # 配置使用的数据库引擎
|
||
"NAME": "ky", # 数据库的名称
|
||
"USER": "root", # 数据库用户名
|
||
"PASSWORD": "moon1991", # 数据库密码
|
||
"HOST": "47.108.232.131", # 数据库服务器地址
|
||
"PORT": "3306", # 数据库服务器端口号,MySQL默认为3306
|
||
}
|
||
}
|
||
|
||
# redis
|
||
CACHES = {
|
||
"default": { # 配置默认缓存
|
||
"BACKEND": "django_redis.cache.RedisCache", # 配置使用的缓存引擎
|
||
"LOCATION": "redis://127.0.0.1:16379", # 配置缓存的位置
|
||
"OPTIONS": { # 配置缓存的参数
|
||
"db": 0,
|
||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||
# "PASSWORD": "", # Redis的密码
|
||
# "TIMEOUT": 5, # 连接超时设置
|
||
# 'CONNECTION_POOL_KWARGS': {
|
||
# 'max_connections': 100, # 设置最大连接数
|
||
# },
|
||
},
|
||
},
|
||
"queue": { # 配置队列
|
||
"BACKEND": "django_redis.cache.RedisCache",
|
||
"LOCATION": "redis://127.0.0.1:16379",
|
||
"OPTIONS": {
|
||
"db": 1,
|
||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||
},
|
||
},
|
||
"base": { # 配置基础缓存
|
||
"BACKEND": "django_redis.cache.RedisCache",
|
||
"LOCATION": "redis://127.0.0.1:16379",
|
||
"OPTIONS": {
|
||
"db": 2,
|
||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||
},
|
||
},
|
||
"cache": { # 配置缓存
|
||
"BACKEND": "django_redis.cache.RedisCache",
|
||
"LOCATION": "redis://127.0.0.1:16379",
|
||
"OPTIONS": {
|
||
"db": 3,
|
||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||
},
|
||
},
|
||
}
|
||
|
||
# Password validation
|
||
# https://docs.djangoproject.com/en/5.1/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/5.1/topics/i18n/
|
||
|
||
LANGUAGE_CODE = "zh-hans"
|
||
|
||
TIME_ZONE = "Asia/Shanghai" # 配置时区 UTC
|
||
|
||
USE_I18N = True
|
||
|
||
# 配置支持的语言,例如英语和中文
|
||
LANGUAGES = [
|
||
("en", "English"),
|
||
("zh-hans", "简体中文"),
|
||
]
|
||
|
||
# 配置翻译文件所在路径
|
||
LOCALE_PATHS = [
|
||
BASE_DIR / "locale",
|
||
]
|
||
|
||
USE_TZ = False # 设置为 False,使用本地时间
|
||
|
||
# Static files (CSS, JavaScript, Images)
|
||
# https://docs.djangoproject.com/en/5.1/howto/static-files/
|
||
|
||
STATIC_URL = "static/"
|
||
|
||
# Default primary key field type
|
||
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
|
||
|
||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||
|
||
from mongoengine import connect
|
||
|
||
connect(db="wk", host="47.108.232.131", port=27017, username="wk", password="moon1991")
|
||
|
||
# rest_framework配置
|
||
REST_FRAMEWORK = {
|
||
"DATETIME_FORMAT": "%Y-%m-%d %H:%M:%S", # 时间格式
|
||
"DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",), # 过滤器
|
||
"DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema", # 自动生成api文档
|
||
"DEFAULT_RENDERER_CLASSES": (
|
||
"rest_framework.renderers.JSONRenderer", # 返回json格式
|
||
"rest_framework.renderers.BrowsableAPIRenderer", # 返回html格式
|
||
),
|
||
}
|
||
|
||
# log 配置部分BEGIN #
|
||
SERVER_LOGS_FILE = os.path.join(BASE_DIR, "logs", "server.log")
|
||
ERROR_LOGS_FILE = os.path.join(BASE_DIR, "logs", "error.log")
|
||
LOGS_FILE = os.path.join(BASE_DIR, "logs")
|
||
if not os.path.exists(os.path.join(BASE_DIR, "logs")):
|
||
os.makedirs(os.path.join(BASE_DIR, "logs"))
|
||
|
||
# 格式:[2020-04-22 23:33:01][micoservice.apps.ready():16] [INFO] 这是一条日志:
|
||
# 格式:[日期][模块.函数名称():行号] [级别] 信息
|
||
STANDARD_LOG_FORMAT = "[%(asctime)s][%(name)s.%(funcName)s():%(lineno)d] [%(levelname)s] %(message)s"
|
||
CONSOLE_LOG_FORMAT = "[%(asctime)s][%(name)s.%(funcName)s():%(lineno)d] [%(levelname)s] %(message)s"
|
||
LOGGING = {
|
||
"version": 1,
|
||
"disable_existing_loggers": False,
|
||
"formatters": {
|
||
"standard": {"format": STANDARD_LOG_FORMAT},
|
||
"console": {
|
||
"format": CONSOLE_LOG_FORMAT,
|
||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||
},
|
||
"file": {
|
||
"format": CONSOLE_LOG_FORMAT,
|
||
"datefmt": "%Y-%m-%d %H:%M:%S",
|
||
},
|
||
},
|
||
"handlers": {
|
||
"file": {
|
||
"level": "INFO",
|
||
"class": "logging.handlers.RotatingFileHandler",
|
||
"filename": SERVER_LOGS_FILE,
|
||
"maxBytes": 1024 * 1024 * 100, # 100 MB
|
||
"backupCount": 5, # 最多备份5个
|
||
"formatter": "standard",
|
||
"encoding": "utf-8",
|
||
},
|
||
"error": {
|
||
"level": "ERROR",
|
||
"class": "logging.handlers.RotatingFileHandler",
|
||
"filename": ERROR_LOGS_FILE,
|
||
"maxBytes": 1024 * 1024 * 100, # 100 MB
|
||
"backupCount": 3, # 最多备份3个
|
||
"formatter": "standard",
|
||
"encoding": "utf-8",
|
||
},
|
||
"console": {
|
||
"level": "DEBUG",
|
||
"class": "logging.StreamHandler",
|
||
"formatter": "console",
|
||
},
|
||
},
|
||
"loggers": {
|
||
"": {
|
||
"handlers": ["console", "error", "file"],
|
||
"level": "INFO",
|
||
},
|
||
"django": {
|
||
"handlers": ["console", "error", "file"],
|
||
"level": "INFO",
|
||
"propagate": False,
|
||
},
|
||
"django.db.backends": {
|
||
"handlers": ["console", "error", "file"],
|
||
"propagate": False,
|
||
"level": "DEBUG",
|
||
},
|
||
"uvicorn.error": {
|
||
"level": "INFO",
|
||
"handlers": ["console", "error", "file"],
|
||
},
|
||
"uvicorn.access": {"handlers": ["console", "error", "file"], "level": "INFO"},
|
||
},
|
||
}
|