123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- """
- Django settings for backend project.
- Generated by 'django-admin startproject' using Django 4.2.
- For more information on this file, see
- https://docs.djangoproject.com/en/4.2/topics/settings/
- For the full list of settings and their values, see
- https://docs.djangoproject.com/en/4.2/ref/settings/
- """
- 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/4.2/howto/deployment/checklist/
- # SECURITY WARNING: keep the secret key used in production secret!
- SECRET_KEY = 'django-insecure-1wvwk*vqn21we=)usoe)+w13!+4-sq3_g@0-&j-wa0#lq6yf1='
- # SECURITY WARNING: don't run with debug turned on in production!
- DEBUG = True
- ALLOWED_HOSTS = ['localhost', '127.0.0.1', '8.149.247.53']
- # Application definition
- INSTALLED_APPS = [
- 'corsheaders',
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- 'django_apscheduler',
- 'rest_framework',
- 'rest_framework.authtoken',
- 'api',
- ]
- REST_FRAMEWORK = {
- 'DEFAULT_AUTHENTICATION_CLASSES': ['api.tokenAuthentication.TokenAuthentication'],
- 'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.IsAuthenticated'],
- 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
- 'NON_FIELD_ERRORS_KEY': 'message',
- }
- MIDDLEWARE = [
- 'django.middleware.security.SecurityMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'api.middleware.TokenSessionMiddleware',
- 'corsheaders.middleware.CorsMiddleware',
- 'django.middleware.common.CommonMiddleware',
- 'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
- 'django.contrib.messages.middleware.MessageMiddleware',
- 'django.middleware.clickjacking.XFrameOptionsMiddleware',
- ]
- CORS_ORIGIN_ALLOW_ALL = False
- CORS_ALLOW_METHODS=("*")
- CORS_ALLOW_CREDENTIALS = True
- # 放置生产环境前端host
- CORS_ALLOWED_ORIGINS = []
- if DEBUG:
- CORS_ORIGIN_ALLOW_ALL = True
- AUTH_USER_MODEL = "api.User"
- ROOT_URLCONF = 'backend.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 = 'backend.wsgi.application'
- # Database
- # https://docs.djangoproject.com/en/4.2/ref/settings/#databases
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': BASE_DIR / 'db.sqlite3',
- }
- }
- # Password validation
- # https://docs.djangoproject.com/en/4.2/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/4.2/topics/i18n/
- LANGUAGE_CODE = 'en-us'
- TIME_ZONE = 'Asia/Shanghai'
- USE_I18N = True
- USE_TZ = False
- # Static files (CSS, JavaScript, Images)
- # https://docs.djangoproject.com/en/4.2/howto/static-files/
- STATIC_URL = 'static/'
- # Default primary key field type
- # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
- DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
- # settings.py
- # 日志输出,带颜色
- import colorama
- import logging
- colorama.init()
- class ColoredFormatter(logging.Formatter):
- # ANSI 颜色代码
- COLOR_CODES = {
- logging.DEBUG: '\033[37m', # 白色
- logging.INFO: '\033[92m', # 绿色
- logging.WARNING: '\033[93m', # 黄色
- logging.ERROR: '\033[91m', # 红色
- logging.CRITICAL: '\033[41m' # 红底白字
- }
- RESET_CODE = '\033[0m'
- def format(self, record):
- # 添加颜色代码
- color = self.COLOR_CODES.get(record.levelno, '')
- message = super().format(record)
- return f"{color}{message}{self.RESET_CODE}"
- LOGGING = {
- 'version': 1,
- 'disable_existing_loggers': False,
- 'formatters': {
- 'verbose': {
- # 包含 logger 名称、时间、模块、进程、消息等级 和 消息内容
- 'format': '{levelname} {asctime} {module} {process:d} {thread:d} [{name}] {message}',
- 'style': '{', # 使用花括号风格(Python 3.2+)
- },
- 'simple': {
- # 仅包含 logger 名称、等级 和 消息内容(开发环境推荐)
- '()': ColoredFormatter,
- 'format': '[{name}] {levelname} {message}',
- 'style': '{',
- },
- },
- 'root': {
- 'handlers': ['console'],
- 'level': 'INFO',
- },
- 'handlers': {
- 'console': {
- 'class': 'logging.StreamHandler',
- 'formatter': 'simple', # 使用简洁格式
- },
- },
- 'loggers': {
- 'apscheduler': {
- 'level': 'WARNING',
- },
- }
- }
|