Django ViewClass 2-2) Basic ViewClass, TemplateView

Turtleยท2023๋…„ 12์›” 7์ผ
0
post-thumbnail

๐Ÿ“ŒView(Class)

  • Django์—์„œ ๋ทฐ์— ํ•„์š”ํ•œ ๋‚ด์šฉ๋“ค์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค

  • ๋‹ค๋ฅธ ํ™•์žฅ ๋ทฐ ํด๋ž˜์Šค๋“ค(TemplateView, CreateView ๋“ฑ)์˜ ์ตœ์ƒ์œ„ ๋ถ€๋ชจ ํด๋ž˜์Šค

๐Ÿ“ŒTemplateView

  • View ํด๋ž˜์Šค์—์„œ ํ™•์žฅํ•ด์„œ Template ๋ Œ๋”๋ง์„ ์ตœ์†Œํ•œ์˜ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” Django ํด๋ž˜์Šค

  • template_name ๋ฉค๋ฒ„ ํ•„๋“œ์— ํ…œํ”Œ๋ฆฟ์„ ์ง€์ •

๋ทฐ ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ

class View:
	def get(self, request: HttpRequest):
    	return response: HttpResponse
    
    def post(self, request: HttpRequest):
    	return response: HttpResponse
 
class TemplateView:
	template_name: str
    
    def get_context_data(self):
    	return context: dict

๋ทฐ์— ๋”ฐ๋ฅธ URLMapping

"""taskproject URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
  • urls.py๋ฅผ ๋ณด๋ฉด FBV, CBV์˜ ๊ฒฝ์šฐ ๋ฐ ์ตœ์ƒ์œ„ URLConf ์„ค์ • ๋“ฑ์— ๋Œ€ํ•œ ์˜ˆ์‹œ๊ฐ€ ์•„์ฃผ ์ž˜ ๋‚˜์™€์žˆ์œผ๋‹ˆ ์ด๋ฅผ ์ฐธ๊ณ ํ•ด๋„ ๋œ๋‹ค.

๐Ÿ“ŒDjango TemplateView

from django.views.generic.base import TemplateView
from articles.models import Article

class HomePageView(TemplateView):
    template_name = "home.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["latest_articles"] = Article.objects.all()[:5]
        return context
  • TemplateView๋Š” URL์— ์บก์ฒ˜๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํฌํ•จ๋œ ์ปจํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ ํ…œํ”Œ๋ฆฟ์„ ๋ Œ๋”๋งํ•œ๋‹ค.

  • TemplateView๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ GET๋ฉ”์†Œ๋“œ๋ฅผ ์œ„ํ•œ ๋ทฐ๋กœ POST๋ฉ”์†Œ๋“œ๋Š” ๋”ฐ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

  • get_context_data๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…œํ”Œ๋ฆฟ์— ๋„˜๊ฒจ์ค„ ๋ณ€์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

  • ์ด ๋ทฐ๋Š” ์•„๋ž˜์˜ ๋ฉ”์†Œ๋“œ์™€ ์†์„ฑ์„ ์ƒ์†๋ฐ›๋Š”๋‹ค.

    • django.views.generic.base.TemplateResponseMixin
    • django.views.generic.base.ContextMixin
    • django.views.generic.base.View

๐Ÿ“Œ์ ์šฉ

from django.http import HttpRequest, HttpResponse
from django.shortcuts import render, HttpResponseRedirect, redirect
from django.views.generic import TemplateView, CreateView, UpdateView, DeleteView, ListView, DetailView, FormView
from .models import Task, ChecklistItem
from django.utils import timezone
from django.core.paginator import Paginator
from django.urls import reverse_lazy

class TaskListView(TemplateView):
    template_name = 'pages/task_list.html'
    
    def get_context_data(self, **kwargs):
        tasks = Task.objects.all()
        return {
            'tasks' : tasks,
        }

0๊ฐœ์˜ ๋Œ“๊ธ€