From ca4b52217f16a3735934ceca04c550e41f53a1ec Mon Sep 17 00:00:00 2001 From: XingQue Date: Tue, 14 Jul 2026 02:57:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=9C=AC=E5=9C=B0=20Vite(517?= =?UTF-8?q?3/127.0.0.1)=20=E8=B7=A8=E5=9F=9F=E8=AE=BF=E9=97=AE=E7=BA=BF?= =?UTF-8?q?=E4=B8=8A=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充 CORS 与 CSRF_TRUSTED_ORIGINS,便于客服后台 localhost 联调。 Co-authored-by: Cursor --- a_long_dianjing/settings.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/a_long_dianjing/settings.py b/a_long_dianjing/settings.py index 8488a12..8fc7307 100644 --- a/a_long_dianjing/settings.py +++ b/a_long_dianjing/settings.py @@ -188,18 +188,20 @@ REST_FRAMEWORK = { } -# CORS settings +# CORS settings(本地 Vite 客服后台 + 线上域名) CORS_ALLOWED_ORIGINS = [ - "http://localhost:8080", # 假设前端开发服务器地址 + "http://localhost:8080", "http://127.0.0.1:8080", + "http://localhost:5173", + "http://127.0.0.1:5173", + "http://localhost:5174", + "http://127.0.0.1:5174", + "http://localhost:3000", + "http://127.0.0.1:3000", "http://121.4.54.129", "https://wenhe.nmslb.com:9187", "https://wenhe.nmslb.com:4321", - "http://localhost:5173", - "http://localhost:5174" - - - + "https://wenhe.nmslb.com", ] CORS_ALLOW_CREDENTIALS = True CORS_ALLOW_METHODS = ( @@ -222,6 +224,19 @@ CORS_ALLOW_HEADERS = [ "x-requested-with", ] +# Django 4+:前端带 Cookie/凭证跨域访问时需要信任来源 +CSRF_TRUSTED_ORIGINS = [ + "http://localhost:5173", + "http://127.0.0.1:5173", + "http://localhost:5174", + "http://127.0.0.1:5174", + "http://localhost:8080", + "http://127.0.0.1:8080", + "https://wenhe.nmslb.com", + "https://wenhe.nmslb.com:9187", + "https://wenhe.nmslb.com:4321", +] + # 【待确认】自定义用户模型,你需要在 yonghu/models.py 中创建 class Yonghu #AUTH_USER_MODEL = 'yonghu.Yonghu'