From 3523b38e0b8aea13892bef352fdd37737393342a Mon Sep 17 00:00:00 2001 From: "Andrey K. Choi" Date: Tue, 25 Nov 2025 07:39:21 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix=20Django=204.0+=20CSRF=5FTRU?= =?UTF-8?q?STED=5FORIGINS=20and=20CI=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Fixed issues: - Added proper CSRF_TRUSTED_ORIGINS with schemes (http://, https://) - Added missing sys import in test settings - Updated ALLOWED_HOSTS to include 'postgres' container - Removed duplicate database creation in CI pipeline - Fixed empty CSRF_TRUSTED_ORIGINS causing Django 4.0.E001 error 🐳 CI/CD improvements: - Database container properly referenced in settings - Test environment variables correctly configured - Eliminated database creation conflicts Ready for trusted repository CI/CD execution! --- .drone.yml | 6 ++---- smartsoltech/settings_test.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.drone.yml b/.drone.yml index 580ff86..201566f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -60,7 +60,7 @@ steps: DATABASE_URL: postgresql://postgres:postgres@postgres:5432/smartsoltech_test SECRET_KEY: test-secret-key-for-ci-very-long-and-secure-key-12345 DEBUG: "False" - ALLOWED_HOSTS: localhost,127.0.0.1 + ALLOWED_HOSTS: localhost,127.0.0.1,postgres DJANGO_SETTINGS_MODULE: smartsoltech.settings_test commands: - apt-get update && apt-get install -y libpq-dev gcc curl postgresql-client @@ -70,8 +70,6 @@ steps: - sleep 15 - echo "Checking database connection..." - until pg_isready -h postgres -p 5432 -U postgres; do echo "Waiting for postgres..."; sleep 2; done - - echo "Creating test database..." - - PGPASSWORD=postgres createdb -h postgres -U postgres smartsoltech_test || echo "Database already exists" - echo "Checking migrations..." - cd smartsoltech - python manage.py check --settings=smartsoltech.settings_test @@ -87,7 +85,7 @@ steps: DATABASE_URL: postgresql://postgres:postgres@postgres:5432/smartsoltech_test SECRET_KEY: test-secret-key-for-ci-very-long-and-secure-key-12345 DEBUG: "False" - ALLOWED_HOSTS: localhost,127.0.0.1 + ALLOWED_HOSTS: localhost,127.0.0.1,postgres TELEGRAM_BOT_TOKEN: test-token-for-ci DJANGO_SETTINGS_MODULE: smartsoltech.settings_test commands: diff --git a/smartsoltech/settings_test.py b/smartsoltech/settings_test.py index e641d8c..4d2bd77 100644 --- a/smartsoltech/settings_test.py +++ b/smartsoltech/settings_test.py @@ -1,12 +1,9 @@ # -*- coding: utf-8 -*- import os +import sys import dj_database_url from .settings import * -# Переопределяем настройки для CI/CD тестирования - -print("ALLOWED_HOSTS:", ALLOWED_HOSTS) -print("CSRF_TRUSTED_ORIGINS:", CSRF_TRUSTED_ORIGINS) print("🧪 Test settings loaded") # База данных для тестирования @@ -14,7 +11,7 @@ DATABASES = { 'default': dj_database_url.config( default=os.environ.get( 'DATABASE_URL', - 'postgresql://postgres:postgres@postgres_test:5432/smartsoltech_test' + 'postgresql://postgres:postgres@postgres:5432/smartsoltech_test' ) ) } @@ -33,6 +30,15 @@ DEBUG = os.environ.get('DEBUG', 'False').lower() in ['true', '1', 'yes'] # Разрешенные хосты для CI ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost,127.0.0.1,postgres,*').split(',') + +# CSRF настройки для тестирования +CSRF_TRUSTED_ORIGINS = [ + 'http://localhost', + 'http://127.0.0.1', + 'http://postgres', + 'https://smartsoltech.kr' +] + print("🌐 Allowed hosts:", ALLOWED_HOSTS) # Упрощенный хеширователь паролей для быстрых тестов