View2 [3주차-3] 장고를 활용한 API서버 만들기 - Class 기반의 views Class 기반의 views HTTP Method로 view를 만들 때는 분기문을 사용하여 코드를 짰는데, class를 이용하면 분기문(if 문)을 사용할 때보다 코드가 더 간결해진다. 또한 class를 통해 구현하면 장고 rest_framework에서 제공하는 다양한 다른 class들을 활용해서 반복되는 코드를 작성할 필요 없이 쉽게 view를 만들 수 있다(class 상속 받기). 따라서 대부분의 경우에 class를 통해 구현한다고 한다. polls_api/views.py에서 from rest_framework.views import APIView class QuestionList(APIView): def get(self, request): questions = Question.objects.all(.. 2023. 4. 29. [3주차-2] 장고를 활용한 API서버 만들기 - view 다루기 view polls/views.py에서 from .models import * from django.shortcuts import render def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] #날짜 순으로 최신 5개 질문 리스트 context = {'first_question': latest_question_list[0]} #첫번째 질문 딕셔너리 return render(request, 'polls/index.html', context) #render는 html파일을 그려주는 역할 polls/templates/polls/index.html에서 참고) html에서 변수를 인식하려면 {{변수}}꼴이어야 .. 2023. 4. 29. 이전 1 다음