123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- """
- Django settings for config project.
- Generated by 'django-admin startproject' using Django 5.2.1.
- For more information on this file, see
- https://docs.djangoproject.com/en/5.2/topics/settings/
- For the full list of settings and their values, see
- https://docs.djangoproject.com/en/5.2/ref/settings/
- """
- import os
- from pathlib import Path
- from datetime import timedelta # 确保导入timedelta
- # 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.2/howto/deployment/checklist/
- # SECURITY WARNING: keep the secret key used in production secret!
- SECRET_KEY = "django-insecure-12o%i9mzoyn8ua+j#l!8fed0-4d&y$x3%vtk00y82@97f9702+"
- # 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',
- # Third-party apps
- 'rest_framework',
- 'rest_framework_simplejwt',
- 'corsheaders', # <<<< 1. 添加 corsheaders 到 INSTALLED_APPS
- # Your apps
- # 推荐使用完整的AppConfig路径
- 'accounts.apps.AccountsConfig', # 假设你的 accounts/apps.py 中有 AccountsConfig
- 'groups.apps.GroupsConfig', # 假设你的 groups/apps.py 中有 GroupsConfig
- ]
- MIDDLEWARE = [
- 'corsheaders.middleware.CorsMiddleware', # <<<< 2. 添加 CorsMiddleware, 确保在 CommonMiddleware 之前
- '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',
- ]
- ROOT_URLCONF = "config.urls"
- TEMPLATES = [
- {
- "BACKEND": "django.template.backends.django.DjangoTemplates",
- "DIRS": [], # 如果你有项目级的模板文件夹,可以添加到这里
- "APP_DIRS": True,
- "OPTIONS": {
- "context_processors": [
- "django.template.context_processors.debug", # 之前这里没有 debug,加上比较好
- "django.template.context_processors.request",
- "django.contrib.auth.context_processors.auth",
- "django.contrib.messages.context_processors.messages",
- ],
- },
- },
- ]
- WSGI_APPLICATION = "config.wsgi.application"
- # Database
- # https://docs.djangoproject.com/en/5.2/ref/settings/#databases
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.postgresql',
- 'NAME': 'tongqu_v2_db',
- 'USER': 'postgres',
- 'PASSWORD': '20030316', # 请确保这是你正确的PostgreSQL密码
- 'HOST': 'localhost',
- 'PORT': '5432',
- }
- }
- # Password validation
- # https://docs.djangoproject.com/en/5.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/5.2/topics/i18n/
- LANGUAGE_CODE = 'zh-hans'
- TIME_ZONE = 'Asia/Shanghai'
- USE_I18N = True
- USE_TZ = True
- # Static files (CSS, JavaScript, Images)
- # https://docs.djangoproject.com/en/5.2/howto/static-files/
- STATIC_URL = 'static/'
- STATICFILES_DIRS = [
- os.path.join(BASE_DIR, 'static'), # 项目级static文件夹 (你需要在 BASE_DIR 下创建这个文件夹)
- ]
- # `collectstatic` 命令收集静态文件到这个目录,主要用于生产环境
- STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles_collected')
- # Media files (User uploaded content)
- MEDIA_URL = '/media/'
- MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # (你需要在 BASE_DIR 下创建这个文件夹)
- # Default primary key field type
- # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
- DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
- # Custom User Model
- AUTH_USER_MODEL = 'accounts.CustomUser'
- # Django REST framework settings
- REST_FRAMEWORK = {
- 'DEFAULT_AUTHENTICATION_CLASSES': (
- 'rest_framework_simplejwt.authentication.JWTAuthentication',
- ),
- 'DEFAULT_PERMISSION_CLASSES': (
- 'rest_framework.permissions.IsAuthenticated',
- )
- # 其他DRF设置...
- }
- # Simple JWT settings
- SIMPLE_JWT = {
- 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
- 'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
- 'UPDATE_LAST_LOGIN': True,
- 'AUTH_HEADER_TYPES': ('Bearer',),
- 'USER_ID_FIELD': 'id',
- 'USER_ID_CLAIM': 'user_id',
- 'SIGNING_KEY': SECRET_KEY, # 使用Django的SECRET_KEY
- # 其他SimpleJWT设置...
- }
- # CORS (Cross-Origin Resource Sharing) settings <<<< 3. 添加CORS配置块
- CORS_ALLOWED_ORIGINS = [
- "http://localhost:5173", # 你Vite前端开发服务器的地址
- "http://127.0.0.1:5173", # 有时也需要加上这个
- # "https://yourfrontenddomain.com", # 生产环境前端域名
- ]
- # 或者,仅在绝对的开发环境中,如果你想允许所有来源 (非常不推荐用于生产)
- # CORS_ALLOW_ALL_ORIGINS = True # 如果使用这个,上面的 CORS_ALLOWED_ORIGINS 会被忽略
- # 如果前端需要发送凭据 (如cookies或HTTP认证头) 进行跨域请求
- # 并且你的axios配置了 withCredentials: true
- # CORS_ALLOW_CREDENTIALS = True
- CORS_ALLOW_METHODS = [
- "DELETE",
- "GET",
- "OPTIONS",
- "PATCH",
- "POST",
- "PUT",
- ]
- CORS_ALLOW_HEADERS = [
- "accept",
- "accept-encoding",
- "authorization", # 允许 Authorization 头
- "content-type",
- "dnt",
- "origin",
- "user-agent",
- "x-csrftoken",
- "x-requested-with",
- ]
|