사용자 도구

사이트 도구


python:django

Django

문서

Django의 LTS버전은 X.2로 배포됨.( 3.2, 4.2, 5,2 )

장고 사용

주요명령어

  • 장고 프로젝트생성
django-admin startproject "프로젝트명"
  • 자주사용하는 명령어
python manage.py startapp "Application Name"
python manage.py runserver 
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic
python manage.py inspectdb

앱작성

  • 새로운 앱을 작성
$ python manage.py startapp plant
  • Web Application에 등록
settings.py
INSTALLED_APPS = [
    'plant.apps.PlantConfig',
    ....
]
  • 정의된 모델을 Database에서 반영
$ python manage.py makemigrations polls
  • 반영된 model의 Table script보기
$ python manage.py sqlmigrate polls 0001

migrate

명령을 통해 마이그레이션을 수행하거나 데이터베이스를 건드리지 않고도 프로젝트의 문제를 확인할 수 있습니다.

$ python manage.py check 
$ python manage.py makemigrations
$ python manage.py migrate

migrate 명령은 아직 적용되지 않은 마이그레이션을 모두 수집해 이를 실행하며(Django는 django_migrations 테이블을 두어 마이그레이션 적용 여부를 추적합니다) 이 과정을 통해 모델에서의 변경 사항들과 데이터베이스의 스키마의 동기화가 이루어집니다.

  • (models.py 에서) 모델을 변경합니다.
  • python manage.py makemigrations을 통해 이 변경사항에 대한 마이그레이션을 만드세요.
  • python manage.py migrate 명령을 통해 변경사항을 데이터베이스에 적용하세요.

마이그레이션을 만드는 명령과 적용하는 명령이 분리된 것은 버전 관리 시스템에 마이그레이션을 커밋하고 앱과 함께 출시할 수 있도록 하기 위해서입니다. 이는 당신의 개발을 쉽게 해줄 뿐 아니라, 다른 개발자가 프로덕션에서 사용할 수 있게 해줍니다.

API가지고 놀기

대화식 Python 쉘에 뛰어들어 Django API를 자유롭게 가지고 놀아봅시다. Python 쉘을 실행하려면 다음의 명령을 입력합니다.

$ python manage.py shell
>>> from polls.models import Choice, Question  # Import the model classes we just wrote.
 
# No questions are in the system yet.
>>> Question.objects.all()
<QuerySet []>
 
# Create a new Question.
# Support for time zones is enabled in the default settings file, so
# Django expects a datetime with tzinfo for pub_date. Use timezone.now()
# instead of datetime.datetime.now() and it will do the right thing.
>>> from django.utils import timezone
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
 
# Save the object into the database. You have to call save() explicitly.
>>> q.save()
 
# Now it has an ID.
>>> q.id
1
 
# Access model field values via Python attributes.
>>> q.question_text
"What's new?"
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
 
# Change values by changing the attributes, then calling save().
>>> q.question_text = "What's up?"
>>> q.save()
 
# objects.all() displays all the questions in the database.
>>> Question.objects.all()
<QuerySet [<Question: Question object (1)>]>

모델의 관계에 대한 더 많은 정보는 관련 객체에 접근하기를 참조하세요. API에서 이중 밑줄(__) 을 이용해서 어떻게 필드를 조회할 수 있는지는 필드 조회를 읽어보세요.데이터베이스 API에 대한 자세한 내용을 보려면, 데이터베이스 API 레퍼런스를 읽어보세요.

python/django.txt · 마지막으로 수정됨: 2025/10/18 13:52 저자 taekgu