Django의 settings.py 템플릿 설정이에요!
# settings.py
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [], # 👈 여기!
"APP_DIRS": True,
...
}
]
"HTML 템플릿 파일을 어디서 찾을지" 경로를 지정하는 설정이에요!
# ❌ DIRS 비어있음
"DIRS": []
# Django가 templates 폴더를 못 찾을 수 있음
# ✅ DIRS 경로 지정
"DIRS": [BASE_DIR / "templates"]
# Django가 templates 폴더를 찾아서 HTML 렌더링 가능
my_project/
├── templates/ ← HTML 파일 모아두는 폴더
│ ├── index.html
│ ├── users.html
│ └── items.html
├── app/
│ └── views.py
└── settings.py
# settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES = [
{
"DIRS": [BASE_DIR / "templates"], # 👈 templates 폴더 지정
}
]
DIRS = Directories (디렉토리들)
↑
폴더 경로들의 목록
# 여러 개도 가능!
"DIRS": [
BASE_DIR / "templates", # 메인 템플릿
BASE_DIR / "app" / "templates", # 앱 템플릿
]
# Django - DIRS로 HTML 경로 지정
"DIRS": [BASE_DIR / "templates"]
# FastAPI - Jinja2로 HTML 경로 지정
from fastapi.templating import Jinja2Templates
templates = Jinja2Templates(directory="templates")
| 항목 | 설명 |
|---|---|
| DIRS 의미 | Directories (폴더 경로 목록) |
| 역할 | HTML 템플릿 파일 위치 지정 |
| 비워두면 | 앱 내부 templates만 탐색 |
| 경로 지정시 | 프로젝트 전체 templates 탐색 |
💡 API 서버만 만들 때는 DIRS 거의 안써요!
HTML을 직접 렌더링하는
웹 서비스 만들 때 주로 사용해요!