""" Django settings for flower project. Generated by 'django-admin startproject' using Django 5.2.7. 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/ """ # 兼容旧版 django-qiniu-storage try: from django.utils.encoding import force_text except ImportError: from django.utils.encoding import force_str as force_text import django.utils.encoding django.utils.encoding.force_text = force_text from datetime import timedelta from pathlib import Path from environ import Env # 应用七牛云 SDK 补丁(必须在导入其他模块之前) from flower.qiniu_patch import apply_all_patches apply_all_patches() env = Env( DEBUG=(bool, False), ) # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # Initialize environment variables Env.read_env(str(BASE_DIR / '.env')) # 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 = env('SECRET_KEY', default='weak_secret_key_for_dev_only') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = env('DEBUG', default=False) ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=[]) # CORS 配置 CORS_ALLOW_ALL_ORIGINS = DEBUG # 开发环境允许所有源,生产环境需要配置白名单 CORS_ALLOWED_ORIGINS = env.list('CORS_ALLOWED_ORIGINS', default=[ 'http://localhost:3000', 'http://localhost:5173', 'http://127.0.0.1:3000', 'http://127.0.0.1:5173', ]) CORS_ALLOW_CREDENTIALS = True # 允许携带凭证(如 Cookie、认证头) CORS_ALLOW_HEADERS = [ 'accept', 'accept-encoding', 'authorization', 'content-type', 'dnt', 'origin', 'user-agent', 'x-csrftoken', 'x-requested-with', ] # Application definition INSTALLED_APPS = [ 'simpleui', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # 使用七牛云存储,不需要staticfiles app 'corsheaders', # CORS 支持 'rest_framework', 'drf_spectacular', 'basic_info', 'stock', 'api_v1', 'api_man', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', # CORS 中间件,必须放在 CommonMiddleware 之前 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticated', ], 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', } # JWT 配置 SIMPLE_JWT = { 'ACCESS_TOKEN_LIFETIME': timedelta(days=7), 'REFRESH_TOKEN_LIFETIME': timedelta(days=7), 'ROTATE_REFRESH_TOKENS': True, 'UPDATE_LAST_LOGIN': True, } SPECTACULAR_SETTINGS = { 'TITLE': 'Flower API', 'DESCRIPTION': 'API documentation for the Flower project', 'VERSION': '1.0.0', 'SERVE_INCLUDE_SCHEMA': False, } ROOT_URLCONF = 'flower.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'flower.wsgi.application' # Database # https://docs.djangoproject.com/en/5.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': env('DB_NAME'), 'USER': env('DB_USER'), 'PASSWORD': env('DB_PASSWORD'), 'HOST': env('DB_HOST', default='localhost'), 'PORT': env('DB_PORT', default='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/ # 七牛云存储配置 QINIU_ACCESS_KEY = 'IySZff0zEN7jHeDuxj5PrQNMv0qjKoVqBRCuFMzi' QINIU_SECRET_KEY = 'Mph5XkjUQO3VHFPT1JulYHY442uMOya2bXjbSZp8' QINIU_BUCKET_NAME = 'yunwenerp' QINIU_BUCKET_DOMAIN = 't5510mjho.hn-bkt.clouddn.com' QINIU_SECURE_URL = False STORAGES = { 'default': { 'BACKEND': 'qiniustorage.backends.QiniuStorage', }, 'staticfiles': { 'BACKEND': 'qiniustorage.backends.QiniuStorage', }, } DEFAULT_FILE_STORAGE = 'qiniustorage.backends.QiniuStorage' STATICFILES_STORAGE = 'qiniustorage.backends.QiniuStorage' # 静态文件URL(直接从七牛云访问) STATIC_URL = f'http://{QINIU_BUCKET_DOMAIN}/static/' STATIC_ROOT = 'static' MEDIA_URL = f'http://{QINIU_BUCKET_DOMAIN}/media/' # Default primary key field type # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'