티스토리 뷰

개발/Python

[django] 장고 시작하기

senni 2020. 7. 20. 14:00

git

git 초기화

$ cat .gitignore
$ git init
$ git add .
$ git commit -m 'init'

venv

virtualenv로 가상환경 생성

$ virtualenv venv

가상환경 실행

$ . venv/Scripts/activate

pip

django, mysql 설치

$ pip install django
$ pip install mysqlclient

pip install 이후 다른 환경에서도 동일하게 패키지를 설치할 수 있도록 설치했던 파일 freeze

$ pip freeze > requirements.txt

requirements.txt 파일에 저장되어있는 패키지 설치

$ pip install -r requirements.txt

startproject

프로젝트 생성

$ django-admin startproject mauritius .

데이터베이스 생성 후 마이그레이션, 슈퍼유저 생성

$ python manage.py makemigrations
$ python manage.py migrate
$ winpty python manage.py createsuperuser

서버 띄우기

$ python manage.py runserver

startapp (앱 마다 반복)

앱 생성

$ python manage.py startapp companies

model.py 편집 후 마이그레이션

$ python manage.py makemigrations companies
$ python manage.py sqlmigrate companies 0001
$ python manage.py migrate

example:

$ python manage.py startapp tags
$ python manage.py makemigrations tags
$ python manage.py sqlmigrate tags 0001
$ python manage.py migrate

관리자 계정 생성

$ python manage.py createsuperuser

runserver

$ python manage.py runserver

ip와 port 지정 가능

$ python manage.py runserver 0.0.0.0:8000

 

migration init(Database 유지)

$ python manage.py showmigrations   # 마이그레이션 기록 조회
$ python manage.py migrate --fake 앱이름 zero   # 각각의 앱에서 마이그레이션 기록 삭제
# 각각의 앱에서 마이그레이션 폴더를 열어 `__init__.py` 파일을 제외한 모든 파일들을 삭제
$ python manage.py makemigrations   # 첫 마이그레이션 생성
$ python manage.py migrate --fake-initial   # 첫 마이그레이션을 페이크

참고자료

'개발 > Python' 카테고리의 다른 글

[Python] Python package 다운로드 사이트  (0) 2019.10.15
댓글