settings.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """
  2. Django settings for backend project.
  3. Generated by 'django-admin startproject' using Django 4.2.
  4. For more information on this file, see
  5. https://docs.djangoproject.com/en/4.2/topics/settings/
  6. For the full list of settings and their values, see
  7. https://docs.djangoproject.com/en/4.2/ref/settings/
  8. """
  9. from pathlib import Path
  10. # Build paths inside the project like this: BASE_DIR / 'subdir'.
  11. BASE_DIR = Path(__file__).resolve().parent.parent
  12. # Quick-start development settings - unsuitable for production
  13. # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
  14. # SECURITY WARNING: keep the secret key used in production secret!
  15. SECRET_KEY = 'django-insecure-1wvwk*vqn21we=)usoe)+w13!+4-sq3_g@0-&j-wa0#lq6yf1='
  16. # SECURITY WARNING: don't run with debug turned on in production!
  17. DEBUG = True
  18. ALLOWED_HOSTS = ['localhost', '127.0.0.1', '8.149.247.53']
  19. # Application definition
  20. INSTALLED_APPS = [
  21. 'corsheaders',
  22. 'django.contrib.admin',
  23. 'django.contrib.auth',
  24. 'django.contrib.contenttypes',
  25. 'django.contrib.sessions',
  26. 'django.contrib.messages',
  27. 'django.contrib.staticfiles',
  28. 'django_apscheduler',
  29. 'rest_framework',
  30. 'rest_framework.authtoken',
  31. 'api',
  32. ]
  33. REST_FRAMEWORK = {
  34. 'DEFAULT_AUTHENTICATION_CLASSES': ['api.tokenAuthentication.TokenAuthentication'],
  35. 'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.IsAuthenticated'],
  36. 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
  37. 'NON_FIELD_ERRORS_KEY': 'message',
  38. }
  39. MIDDLEWARE = [
  40. 'django.middleware.security.SecurityMiddleware',
  41. 'django.contrib.sessions.middleware.SessionMiddleware',
  42. 'api.middleware.TokenSessionMiddleware',
  43. 'corsheaders.middleware.CorsMiddleware',
  44. 'django.middleware.common.CommonMiddleware',
  45. 'django.middleware.csrf.CsrfViewMiddleware',
  46. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  47. 'django.contrib.messages.middleware.MessageMiddleware',
  48. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  49. ]
  50. CORS_ORIGIN_ALLOW_ALL = False
  51. CORS_ALLOW_METHODS=("*")
  52. CORS_ALLOW_CREDENTIALS = True
  53. # 放置生产环境前端host
  54. CORS_ALLOWED_ORIGINS = []
  55. if DEBUG:
  56. CORS_ORIGIN_ALLOW_ALL = True
  57. AUTH_USER_MODEL = "api.User"
  58. ROOT_URLCONF = 'backend.urls'
  59. TEMPLATES = [
  60. {
  61. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  62. 'DIRS': [],
  63. 'APP_DIRS': True,
  64. 'OPTIONS': {
  65. 'context_processors': [
  66. 'django.template.context_processors.debug',
  67. 'django.template.context_processors.request',
  68. 'django.contrib.auth.context_processors.auth',
  69. 'django.contrib.messages.context_processors.messages',
  70. ],
  71. },
  72. },
  73. ]
  74. WSGI_APPLICATION = 'backend.wsgi.application'
  75. # Database
  76. # https://docs.djangoproject.com/en/4.2/ref/settings/#databases
  77. DATABASES = {
  78. 'default': {
  79. 'ENGINE': 'django.db.backends.sqlite3',
  80. 'NAME': BASE_DIR / 'db.sqlite3',
  81. }
  82. }
  83. # Password validation
  84. # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
  85. AUTH_PASSWORD_VALIDATORS = [
  86. {
  87. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  88. },
  89. {
  90. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  91. },
  92. {
  93. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  94. },
  95. {
  96. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  97. },
  98. ]
  99. # Internationalization
  100. # https://docs.djangoproject.com/en/4.2/topics/i18n/
  101. LANGUAGE_CODE = 'en-us'
  102. TIME_ZONE = 'Asia/Shanghai'
  103. USE_I18N = True
  104. USE_TZ = False
  105. # Static files (CSS, JavaScript, Images)
  106. # https://docs.djangoproject.com/en/4.2/howto/static-files/
  107. STATIC_URL = 'static/'
  108. # Default primary key field type
  109. # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
  110. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  111. # settings.py
  112. # 日志输出,带颜色
  113. import colorama
  114. import logging
  115. colorama.init()
  116. class ColoredFormatter(logging.Formatter):
  117. # ANSI 颜色代码
  118. COLOR_CODES = {
  119. logging.DEBUG: '\033[37m', # 白色
  120. logging.INFO: '\033[92m', # 绿色
  121. logging.WARNING: '\033[93m', # 黄色
  122. logging.ERROR: '\033[91m', # 红色
  123. logging.CRITICAL: '\033[41m' # 红底白字
  124. }
  125. RESET_CODE = '\033[0m'
  126. def format(self, record):
  127. # 添加颜色代码
  128. color = self.COLOR_CODES.get(record.levelno, '')
  129. message = super().format(record)
  130. return f"{color}{message}{self.RESET_CODE}"
  131. LOGGING = {
  132. 'version': 1,
  133. 'disable_existing_loggers': False,
  134. 'formatters': {
  135. 'verbose': {
  136. # 包含 logger 名称、时间、模块、进程、消息等级 和 消息内容
  137. 'format': '{levelname} {asctime} {module} {process:d} {thread:d} [{name}] {message}',
  138. 'style': '{', # 使用花括号风格(Python 3.2+)
  139. },
  140. 'simple': {
  141. # 仅包含 logger 名称、等级 和 消息内容(开发环境推荐)
  142. '()': ColoredFormatter,
  143. 'format': '[{name}] {levelname} {message}',
  144. 'style': '{',
  145. },
  146. },
  147. 'root': {
  148. 'handlers': ['console'],
  149. 'level': 'INFO',
  150. },
  151. 'handlers': {
  152. 'console': {
  153. 'class': 'logging.StreamHandler',
  154. 'formatter': 'simple', # 使用简洁格式
  155. },
  156. },
  157. 'loggers': {
  158. 'apscheduler': {
  159. 'level': 'WARNING',
  160. },
  161. }
  162. }