django-admin startproject 명령어를 실행하면 기본적으로 아래와 같은 구조가 생성된다.
myproject/ ← 프로젝트 루트
│
├── manage.py ← 서버 실행, 마이그레이션 등 명령어 도구
│
├── myproject/ ← 프로젝트 설정 폴더
│ ├── __init__.py ← Python 패키지임을 알리는 파일
│ ├── settings.py ← 전체 설정 (DB, 앱 등록 등)
│ ├── urls.py ← 최상위 URL 관리
│ ├── wsgi.py ← 서버 배포용
│ └── asgi.py ← 비동기 서버 배포용
│
└── articles/ ← 앱 폴더
├── migrations/ ← DB 변경 이력 자동 생성
├── templates/ ← HTML 파일 모음
│ └── articles/ ← 앱 이름과 동일한 폴더 (네임스페이스)
│ └── *.html
├── static/ ← CSS, JS, 이미지
├── __init__.py
├── admin.py ← 관리자 페이지 설정
├── apps.py ← 앱 설정
├── models.py ← DB 테이블 정의
├── views.py ← 요청 처리 로직
├── urls.py ← 앱 URL 관리
└── tests.py ← 테스트 코드
MTV : Django가 웹 요청을 처리하는 기본 설계 구조
| 글자 | 이름 | 파일 | 역할 |
|---|---|---|---|
| M | Model | models.py | DB 테이블 정의 & 데이터 관리 |
| T | Template | templates/*.html | 화면 출력 (HTML) |
| V | View | views.py | 요청 받아서 처리하는 로직 |
사용자가 /search/?query=hello 요청
↓
urls.py
(어느 view로 보낼지 결정)
↓
views.py ←→ models.py
(로직 처리) (DB에서 데이터 가져옴)
↓
templates/
(데이터를 HTML에 끼워서 출력)
↓
사용자 브라우저
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', views.index),
path('dinner/', views.dinner),
path('search/', views.search), # /search/ 오면 views.search로 보냄
]
def search(request):
query = request.GET.get('query') # URL에서 ?query=hello 꺼냄
# articles = Article.objects.filter(title=query) # Model로 DB 조회
return render(request, 'articles/search.html') # Template 반환
{% extends 'articles/base.html' %}
{% block content %}
<form action="#" method="GET">
<label for="message">검색어</label>
<input type="text" name="query" id="message">
<input type="submit" value="submit">
</form>
{% endblock content %}
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
myproject/
│
├── manage.py
│
├── myproject/
│ ├── settings.py
│ └── urls.py ← 교통 정리
│
└── articles/
├── migrations/ ← DB 변경 이력
├── templates/
│ └── articles/
│ ├── base.html ← 공통 뼈대 (T)
│ └── search.html ← 화면 출력 (T)
├── models.py ← DB 정의 (M)
├── views.py ← 로직 처리 (V)
└── urls.py ← 앱 URL 관리
Django MTV는 다른 프레임워크의 MVC 패턴이랑 거의 같음. 이
| MVC | MTV | 역할 |
|---|---|---|
| Model | Model | DB |
| View | Template | 화면 |
| Controller | View | 로직 |
전체 흐름 한 줄 정리
URL 요청 → urls.py(연결) → views.py(처리) → models.py(DB) → templates(출력) → 브라우저