본문 바로가기

프로그래밍/Python

장고(DJango)관련 주요 명령어

1. Model Class작성후 DB 및 테이블 생성

프로젝트 폴더에는 manage.py 파일이 존재한다.

> python manage.py makemigrations

   # migrations 폴더에 0001_initial.py 파일이 생긴다.

> python manage.py migrate

   # 실제 models.py에 작성된 모델 클래스에서 작성한 필드를 가진 테이블이 DB에 생성된다.

   # 이 동작은 현재 프로젝트의 settings.py에 등록된 앱들이 사용하는 모델들(models.py)을 참조 하여 DB를 초기화 한다.

   # 이 명령은 모델(models.py)이 변경되었을 경우 반복적으로 실행 시킬 수 있고, 변경된 부분만 다시 반영 된다.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'board',  # 새로 추가한 앱
    'fcuser'
]            

    # 이때 생성되는 데이터베이스는 settings.py에 등록된 DATABASE 딕셔너리(dictionary)에 정의된 엔진과 데이터베이스명을 사용한다.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

 

2. SQLite Command line 명령을 가능하도록 하기

Command창 또는 VSCode내의 터미널 창에서 sqlite command line 명령이 가능하도록 하기 위해서는 먼저 Sqlite tools를 설치 해야 한다.

다운로드 : SQLite 공식 다운로드 페이지

위 다운로드 페이지에서 각 OS별로 제공되는 SQLite-Tools 를 설치하면 된다.

Windows OS의 경우는 sqlite-tools-win32 버전을 다운

압축을 해제 하면 다음과 같이 3개의 실행 파일이 나오는데 폴더를 만들어서 Path를 지정하거나

Windows\System32 디렉토리에 복사해 넣으면 command창에서 사용할 수 있게 된다.

SQLite-tools

그러면 다음과 같이 Command 창에서 sqlite command line 을 실행 할 수 있다

 

 

3. VSCode에서 프로젝트별 웹서버 동작 시키기

해당 프로젝트의 "manage.py"가 있는 프로젝트 경로로 이동 후 다음과 같이 웹서버 실행

      fc_community> python manage.py runserver

       다음과 같이 출력되면 "http://127.0.0.1:8000/" 웹서버로 호출이 가능해 진다.

       System check identified no issues (0 silenced).
       January 02, 2021 - 14:04:59
       Django version 3.1.4, using settings 'fc_community.settings'
       Starting development server at http://127.0.0.1:8000/
       Quit the server with CTRL-BREAK.