settings.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. """
  2. Django settings for config project.
  3. Generated by 'django-admin startproject' using Django 5.2.1.
  4. For more information on this file, see
  5. https://docs.djangoproject.com/en/5.2/topics/settings/
  6. For the full list of settings and their values, see
  7. https://docs.djangoproject.com/en/5.2/ref/settings/
  8. """
  9. import os
  10. from pathlib import Path
  11. from datetime import timedelta # 确保导入timedelta
  12. # Build paths inside the project like this: BASE_DIR / 'subdir'.
  13. BASE_DIR = Path(__file__).resolve().parent.parent
  14. # Quick-start development settings - unsuitable for production
  15. # See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
  16. # SECURITY WARNING: keep the secret key used in production secret!
  17. SECRET_KEY = "django-insecure-12o%i9mzoyn8ua+j#l!8fed0-4d&y$x3%vtk00y82@97f9702+"
  18. # SECURITY WARNING: don't run with debug turned on in production!
  19. DEBUG = True
  20. ALLOWED_HOSTS = []
  21. # Application definition
  22. INSTALLED_APPS = [
  23. 'django.contrib.admin',
  24. 'django.contrib.auth',
  25. 'django.contrib.contenttypes',
  26. 'django.contrib.sessions',
  27. 'django.contrib.messages',
  28. 'django.contrib.staticfiles',
  29. # Third-party apps
  30. 'rest_framework',
  31. 'rest_framework_simplejwt',
  32. 'corsheaders', # <<<< 1. 添加 corsheaders 到 INSTALLED_APPS
  33. # Your apps
  34. # 推荐使用完整的AppConfig路径
  35. 'accounts.apps.AccountsConfig', # 假设你的 accounts/apps.py 中有 AccountsConfig
  36. 'groups.apps.GroupsConfig', # 假设你的 groups/apps.py 中有 GroupsConfig
  37. ]
  38. MIDDLEWARE = [
  39. 'corsheaders.middleware.CorsMiddleware', # <<<< 2. 添加 CorsMiddleware, 确保在 CommonMiddleware 之前
  40. 'django.middleware.security.SecurityMiddleware',
  41. 'django.contrib.sessions.middleware.SessionMiddleware',
  42. 'django.middleware.common.CommonMiddleware',
  43. 'django.middleware.csrf.CsrfViewMiddleware',
  44. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  45. 'django.contrib.messages.middleware.MessageMiddleware',
  46. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  47. ]
  48. ROOT_URLCONF = "config.urls"
  49. TEMPLATES = [
  50. {
  51. "BACKEND": "django.template.backends.django.DjangoTemplates",
  52. "DIRS": [], # 如果你有项目级的模板文件夹,可以添加到这里
  53. "APP_DIRS": True,
  54. "OPTIONS": {
  55. "context_processors": [
  56. "django.template.context_processors.debug", # 之前这里没有 debug,加上比较好
  57. "django.template.context_processors.request",
  58. "django.contrib.auth.context_processors.auth",
  59. "django.contrib.messages.context_processors.messages",
  60. ],
  61. },
  62. },
  63. ]
  64. WSGI_APPLICATION = "config.wsgi.application"
  65. # Database
  66. # https://docs.djangoproject.com/en/5.2/ref/settings/#databases
  67. DATABASES = {
  68. 'default': {
  69. 'ENGINE': 'django.db.backends.postgresql',
  70. 'NAME': 'tongqu_v2_db',
  71. 'USER': 'postgres',
  72. 'PASSWORD': '20030316', # 请确保这是你正确的PostgreSQL密码
  73. 'HOST': 'localhost',
  74. 'PORT': '5432',
  75. }
  76. }
  77. # Password validation
  78. # https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
  79. AUTH_PASSWORD_VALIDATORS = [
  80. {"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",},
  81. {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
  82. {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
  83. {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
  84. ]
  85. # Internationalization
  86. # https://docs.djangoproject.com/en/5.2/topics/i18n/
  87. LANGUAGE_CODE = 'zh-hans'
  88. TIME_ZONE = 'Asia/Shanghai'
  89. USE_I18N = True
  90. USE_TZ = True
  91. # Static files (CSS, JavaScript, Images)
  92. # https://docs.djangoproject.com/en/5.2/howto/static-files/
  93. STATIC_URL = 'static/'
  94. STATICFILES_DIRS = [
  95. os.path.join(BASE_DIR, 'static'), # 项目级static文件夹 (你需要在 BASE_DIR 下创建这个文件夹)
  96. ]
  97. # `collectstatic` 命令收集静态文件到这个目录,主要用于生产环境
  98. STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles_collected')
  99. # Media files (User uploaded content)
  100. MEDIA_URL = '/media/'
  101. MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # (你需要在 BASE_DIR 下创建这个文件夹)
  102. # Default primary key field type
  103. # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
  104. DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
  105. # Custom User Model
  106. AUTH_USER_MODEL = 'accounts.CustomUser'
  107. # Django REST framework settings
  108. REST_FRAMEWORK = {
  109. 'DEFAULT_AUTHENTICATION_CLASSES': (
  110. 'rest_framework_simplejwt.authentication.JWTAuthentication',
  111. ),
  112. 'DEFAULT_PERMISSION_CLASSES': (
  113. 'rest_framework.permissions.IsAuthenticated',
  114. )
  115. # 其他DRF设置...
  116. }
  117. # Simple JWT settings
  118. SIMPLE_JWT = {
  119. 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
  120. 'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
  121. 'UPDATE_LAST_LOGIN': True,
  122. 'AUTH_HEADER_TYPES': ('Bearer',),
  123. 'USER_ID_FIELD': 'id',
  124. 'USER_ID_CLAIM': 'user_id',
  125. 'SIGNING_KEY': SECRET_KEY, # 使用Django的SECRET_KEY
  126. # 其他SimpleJWT设置...
  127. }
  128. # CORS (Cross-Origin Resource Sharing) settings <<<< 3. 添加CORS配置块
  129. CORS_ALLOWED_ORIGINS = [
  130. "http://localhost:5173", # 你Vite前端开发服务器的地址
  131. "http://127.0.0.1:5173", # 有时也需要加上这个
  132. # "https://yourfrontenddomain.com", # 生产环境前端域名
  133. ]
  134. # 或者,仅在绝对的开发环境中,如果你想允许所有来源 (非常不推荐用于生产)
  135. # CORS_ALLOW_ALL_ORIGINS = True # 如果使用这个,上面的 CORS_ALLOWED_ORIGINS 会被忽略
  136. # 如果前端需要发送凭据 (如cookies或HTTP认证头) 进行跨域请求
  137. # 并且你的axios配置了 withCredentials: true
  138. # CORS_ALLOW_CREDENTIALS = True
  139. CORS_ALLOW_METHODS = [
  140. "DELETE",
  141. "GET",
  142. "OPTIONS",
  143. "PATCH",
  144. "POST",
  145. "PUT",
  146. ]
  147. CORS_ALLOW_HEADERS = [
  148. "accept",
  149. "accept-encoding",
  150. "authorization", # 允许 Authorization 头
  151. "content-type",
  152. "dnt",
  153. "origin",
  154. "user-agent",
  155. "x-csrftoken",
  156. "x-requested-with",
  157. ]