VelogaWhite's picture
make db_path's default then change HowToRunInLocal.txt
ec04d48
Raw History Blame Contribute Delete
5.31 kB
"""
Django settings for superlists project.
Generated by 'django-admin startproject' using Django 6.0.
For more information on this file, see
https://docs.djangoproject.com/en/6.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/6.0/ref/settings/
"""
from pathlib import Path
import os
# 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/6.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-yn6!va0-@jjwu)5($7u@w5po_#1_dm_yvoa2g(142(vau5+7jx'
ALLOWED_HOSTS = [
'sdp2-2025-group1-django-tdd-app.hf.space',
'localhost',
'127.0.0.1'
]
# SECURITY WARNING: don't run with debug turned on in production!
if "DJANGO_DEBUG_FALSE" in os.environ:
DEBUG = False
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
# 2. ถ้ามี DJANGO_ALLOWED_HOST ส่งมา ให้เติมเข้าไปใน list เดิม
if "DJANGO_ALLOWED_HOST" in os.environ:
ALLOWED_HOSTS.append(os.environ["DJANGO_ALLOWED_HOST"])
# 3. รับ Path Database จาก Env (สำคัญสำหรับ Podman Mount)
db_path = os.environ.get("DJANGO_DB_PATH", BASE_DIR / "db.sqlite3")
else:
DEBUG = True
SECRET_KEY = "insecure-key-for-dev"
# ไม่ต้องแก้ ALLOWED_HOSTS เพิ่ม
db_path = BASE_DIR / "db.sqlite3"
# Application definition
INSTALLED_APPS = [
# "django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"lists",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
"whitenoise.middleware.WhiteNoiseMiddleware",
'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 = 'superlists.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 = 'superlists.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": db_path
}
}
# Password validation
# https://docs.djangoproject.com/en/6.0/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/6.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/6.0/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / "static"
# อนุญาตให้แสดงผลใน iframe ได้ทุกที่ (แก้เผ็ด Firefox)
X_FRAME_OPTIONS = 'ALLOWALL'
# ==========================================
# ส่วนแก้ไขปัญหา CSRF บน Hugging Face Spaces
# ==========================================
# 1. บอก Django ว่าเราอยู่หลัง Proxy ของ Hugging Face (สำคัญมากเพื่อให้รู้ว่าเป็น HTTPS)
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# 2. ตั้งค่า Cookie ให้ทำงานข้ามโดเมนได้ (แก้ปัญหา Cookie not set ใน Iframe)
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = 'None'
SESSION_COOKIE_SAMESITE = 'None'
# 3. ยืนยัน Trusted Origins (อันเดิมที่คุณแก้ไปแล้ว ใส่ไว้เพื่อความชัวร์)
CSRF_TRUSTED_ORIGINS = [
'https://sdp2-2025-group1-django-tdd-app.hf.space',
'https://huggingface.co'
]
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {"class": "logging.StreamHandler"},
},
"loggers": {
"root": {"handlers": ["console"], "level": "INFO"},
},
}