본문 바로가기

Python/Django

Django project 생성시 자주 쓰는 명령어

728x90

가상환경 설정하기

goto venvs    // collection of virtual env for each product

python3 -m venv  [project_name]

     ex) python3 -m venv facial_recog

 

 

put alias in .profiles

home directory 안의  .profile 에 alias 를 미리 등록하여 두면 log in 후  alias 를 실행하면 virtual 환경으로 쉽게 갈 수 있다.

ex)

  alias facialsite='cd ~/projects/facial_recog;. ~/venvs/facial_recog/bin/activate'    // add this one at the end of .profile file

  enter 'facialsite'  after login 

 

 

Project  creation

django-admin startproject  [project_name] .   // # Note the trailing '.' character. 

ex)  django-admin startproject tutorial .    // tutorial project 가 현재 directory 에 만들어지고 이후 만들어지는 app 은  tutorial 와 동일 level directory 에 만들어 진다

note) 추가 app 은 current direcory 에서 만들면 됨.

 

note)  django-admin startproject  hello_django  와 같이 . 이 없이 project를 creation 하면 hello_django  directory 가 아래  hello_django project 가 만들어 진다.  이 경우는 cd  hello_django 로 하여 app 을 만들면 됨.

 

App Creation

python manage.py startapp  [app_name]

ex) python manage.py startapp  app1  

 

db migration

python manage.py makemigrations
python manage.py migrate

 

특정 model만 migration 할 경우 

python manage.py makemigrations [app_name]

python manage.py migrate   [app_name]  [migration_name] 

// app_name을 지정해서 특정 app만 migrate 할 수 있으며 app_name 뒤에 마이그레이션 파일의 이름을 지정하면 해당 번호(버전)의 마이그레이션을 적용.

 

 

 

superuser creation

python manage.py createsuperuser

 

 

Run project

python manage.py runserver 0.0.0.0:8000   // AWS 와 같은 cloud 에서 실행 할 경우 사용한다
python manage.py runserver  // local machine 에서 사용한다.

 

static data collection

python manage.py collectstatic

note) settings.py 에  STATIC_ROOT = "static_root" 와 같이 선언되어 있어야 명령어가 동작함.

note) production 환경의 admn page 에서 CSS 를 반영시키기 위해서는 위의 명령어가 필요함.

note) settings.py  에서  DEBUG  = False 이면 production 환경임