django 프로젝트만드는것에 이어서 app을 만들어보자.
장고는 한프로젝트에 여러개의 app을 생성 할 수 있다. 반대로 하나의 앱을 여러개의 프로젝트에 적용 할 수도 있다.
app을 생성하자. manage.py가 있는 폴더에서
python manage.py startapp testappapp을 생성했다.
testapp폴더가 생성되고 그안에 파이썬 파일들이 생성된다. 파일의 용도에 대해 알아보자.
models.py - 우리가 사용할 도메인 모델을 정의할때 쓴다.
tests.py - 이름 그대로로 test할때 사용한다.
views.py - spring에서 컨트롤러라고 생각하면 될 것 같다.
settings.py 를 수정하자.
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'testBoard', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': 'root', 'PASSWORD': '', 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 'PORT': '3306', # Set to empty string for default. } }database를 mysql을 사용했다. mysql을 사용하려면 MySQLdb 모듈이 필요하다. 다른 DB도 마찬가로 모듈이 필요하다. sqlite같이 필요없는 것도 있지만 대부분 필요하다. 필요한 모듈을 설치한다. 시간도 맞게 바꿔주고 app을 등록하자.
TIME_ZONE = 'Asia/Seoul' INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'testapp', )이외에 정적파일 url, 디렉토리등을 자신에게 맞게 설정한다. 설정이 끝나면 table을 생성할 수있다.
python manage.py syncdbmodels.py에 모델을 정의하면 테이블이 생성된다. django는 orm을 제공하고있어서 별다른 모듈이 필요없다. 모델을 정의해보자. model은 models.Model을 상속해서 생성한다. 보통의 orm모델 생성 방식과 비슷하다..너무 내생각인가 콜록.. ㅎㅎ
from django.db import models class Board(models.Model): title = models.CharField(max_length=50, blank=True) content = models.CharField(max_length=250, blank=True) writer = models.CharField(max_length=50, blank=True) created_date = models.DateField(null=True, blank=True)table을 생성하기전에 쿼리를 먼저 확인 할 수 있다.
python manage.py sqlall testappsyncdb를 다시 실행하면 table이 생성된다.
0 개의 댓글:
댓글 쓰기