Django 기초 - 2

  • 웹페이지의 기본형식 = CRUD + List

    • C(CREATE)
    • R(READ)
    • U(UPDATE)
    • D(DELETE)

      데이터를 얻을 때…
      단일 객체 = object. -> R,U,D
      복수 객체 = object_list. -> List
      단일 객체로 받을지 복수 객체로 받을지 생각해야 한다.

  • 앱 생성 시 목록

    • admin.py : 모델을 관리하기 위해 등록, 관리자 페이지 커스터마이징
    • apps.py : 앱 정보 관리, 앱 진입 시 로드할 기능 관리
    • models.py : 모델을 만들고 관리하는 부분. (모델은 데이터베이스에 어떤 자료를 어떤 형식으로 저장할 것인지)
    • tests.py : 테스트 셋을 지정하는 파일 - TDD
    • views.py : 페이지나 기능을 코딩하는 파일(대부분 여기서 작업)
  • 뷰 작성하기

    1
    2
    3
    4
    5
    from django.http import HttpResponse


    def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
  • 뷰를 호출하기 위해 앱 디렉토리 안에 urls.py를 생성하고 다음과 같은 코드 작성

    1
    2
    3
    4
    5
    6
    7
    8
    from django.urls import path

    from . import views

    urlpatterns = [
    path('', views.index, name='index'),
    ]
    # path('url pattern', view, name = 'url pattern name')

필요한 값이 들어갈 수 있게 url을 설계한다.

  • config 디렉토리 안에 있는 urls.py에서 앱 디렉토리 안 urls를 바라보도록 설정
    1
    2
    3
    4
    5
    6
    7
    from django.contrib import admin
    from django.urls import include, path

    urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
    ]

include()로 인해 다른 root와 연결하더라도 정상 동작 가능

  • 데이터베이스 생성
    models.py에서 작업한다.
  1. 모델 만들기 : 필드의 종류와 제약조건 설정
  2. 변경 사항 추적하기 : 모델을 읽어서 DB에 반영할 사항을 파일로 작성

    1
    $ python manage.py makemigrations [앱이름]
  3. 변경 사항 적용하기 : 변경 사항 파일을 읽어서 쿼리 실행

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $ python manage.py migrate [앱이름] [마이그레이션 번호]

    # 앱이름을 쓰지 않을 경우 적용은 가능하지만, 지금 변경된 모든 부분이 바뀌게 된다.
    # 변경사항이 만들어진 파일은 migrations 폴더 안에 쌓이게 된다.

    # 장고는 DB 의존성이 낮다.
    # ~에 대한 의존성이 높다. = ~ 가 아니면 정상 동작하지 않는다.
    # ~에 대한 의존성이 낮다. = 그게 아니어도 상관이 없다.

    # 어떤 쿼리가 실행될지 알고 싶다면

    $ python manage.py sqlmigrate polls 0001

    # 어떤 쿼리가 실행될지 확인
    # slow 쿼리인지 여부 확인
    # 튜닝이 필요한 쿼리인지 여부 확인
  • config 디렉토리 안 setting.py에 현재 사용할 앱 추가

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    ]
    # 장고 프로젝트에서 동작할 기능. 사용자들이 사용할 기능.
    # 습관처럼 앱을 추가하면 콤마를 찍자.
  • models.py 변경 후, 변경 사항 적용

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    from django.db import models


    class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


    class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    # 데이터베이스 적용
    # $ python manage.py makemigrations polls
    # $ python manage.py migrate polls
  • 쉘 진입으로 데이터베이스 테스트 가능

    1
    $ python manage.py shell
  • 관리자 생성 및 관리자 서버 접속하기

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ python manage.py createsuperuser

    # Username: 원하는 아이디 입력

    # Email address: 이메일 주소 입력

    # Password: 비밀번호 입력

    # Password (again): 다시 비밀번호 입력

    Superuser created successfully.

    # 접속을 통해 확인
    # $ python manage.py runserver
  • 관리자 페이지에서 사용 중인 앱 변경 가능하도록 만들기

    1
    2
    3
    4
    5
    from django.contrib import admin

    from .models import Question

    admin.site.register(Question)
  • 뷰 추가 작성하기

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    from django.shortcuts import render, get_object_or_404

    from .models import Question,BMI

    def index(request):
    # 모델.objects : 디폴트 매니저
    latest_question_list = Question.objects.order_by('-pub_date')[:5] # pub_date를 내림차순으로 5개 받음
    context = {'latest_question_list': latest_question_list}
    # 템플릿 시스템
    # render(request, 템플릿, 템플릿 변수)
    return render(request, 'polls/index.html', context)

    # 뷰 : 클래스 형 뷰, 함수형 뷰
    # detail 뷰 : 매개변수가 question_id를 받는다.
    # 함수형 뷰는 기본적으로 request라는 매개변수를 받는다.
    def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

    def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

    def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)
  • 템플릿 파일 생성

    • 앱 디렉토리 안에 templates 폴더를 생성한다.
    • templates 폴더 안에 사용하고 있는 앱과 같은 이름을 가진 폴더를 생성한다.
    • 같은 이름을 가진 폴더를 한번 더 생성하는 이유는 배포 과정에서 정확한 영역을 나눠주기 위함이다.(같은 이름을 가진 html 파일이 덮어씌워질 수도 있기 때문이다.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      # index.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Title</title>
      </head>
      <body>
      {% if latest_question_list %}
      <ul>
      {% for question in latest_question_list %}
      <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
      {% endfor %}
      </ul>
      {% else %}
      <p>No polls are available.</p>
      {% endif %}
      </body>
      </html>

      # detail.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Title</title>
      </head>
      <body>
      <h1>{{ question.question_text }}</h1>
      <ul>
      {% for choice in question.choice_set.all %}
      <li>{{ choice.choice_text }}</li>
      {% endfor %}
      </ul>
      </body>
      </html>
Share