1 ๋ถ„ ์†Œ์š”

Generic View๊ฐ€ ๋ญ์ง€?

์ œ๋„ค๋ฆญ ๋ทฐ ๋Š” ์žฅ๊ณ ์—์„œ ์ž์ฃผ ์“ฐ๋Š” ๋ทฐ๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋‚ด์žฅ ๊ธฐ๋Šฅ์ด๋‹ค.

class ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— โ†’ ์ƒ์† ๋ฐ›์•„ ์‚ฌ์šฉํ•จ

์žฅ๊ณ ์˜ ์ œ๋„ค๋ฆญ ๋ทฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปจํ…์ธ ์˜ ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค„ ๋•Œ ์žฅ์ ์ด ๋งŽ์Œ

Generic View ์ข…๋ฅ˜

Base View

๋ทฐ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋‹ค๋ฅธ ์ œ๋„ค๋ฆญ ๋ทฐ์˜ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ๋˜๋Š” ๊ธฐ๋ณธ ์ œ๋„ค๋ฆญ ๋ทฐ

  • View: ์ตœ์ƒ์œ„ ๋ถ€๋ชจ ์ œ๋„ค๋ฆญ ๋ทฐ ํด๋ž˜์Šค
  • TemplateView: ์ฃผ์–ด์ง„ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋ Œ๋”๋ง
  • RedirectView: ์ฃผ์–ด์ง„ URL๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ

Generic Display View

๊ฐ์ฒด์˜ ๋ชฉ๋ก ๋˜๋Š” ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์ƒ์„ธ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ทฐ

  • DetailView: ์กฐ๊ฑด์— ๋งž๋Š” ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์ถœ๋ ฅ
  • ListView: ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ์ฒด ๋ชฉ๋ก ์ถœ๋ ฅ

Generic Edit View

ํผ์„ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๋ทฐ

  • FormView: ํผ์ด ์ฃผ์–ด์ง€๋ฉด ํ•ด๋‹น ํผ์„ ์ถœ๋ ฅ
  • CreateView: ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํผ ์ถœ๋ ฅ
  • UpdateView: ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ํผ์„ ์ถœ๋ ฅ
  • DeleteView: ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•˜๋Š” ํผ์„ ์ถœ๋ ฅ

Generic Date View

  • ArchiveIndexView: ์ง€์ • ๋‚ ์งœ ํ•„๋“œ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋ชฉ๋ก ์ถœ๋ ฅ
  • YearArchiveView: ์ฃผ์–ด์ง„ ์—ฐ๋„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ
  • MonthArchiveView: ์ฃผ์–ด์ง„ ๋‹ฌ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ
  • DayArchiveView: ์ฃผ์–ด์ง„ ๋‚ ์งœ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ
  • TodayArchiveView: ์˜ค๋Š˜ ๋‚ ์งœ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ
  • DateDetailView: ์ฃผ์–ด์ง„ ์—ฐ, ์›”, ์ผ PK์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด ์ถœ๋ ฅ

Generic View Overriding

์†์„ฑ ๋ณ€์ˆ˜ ์˜ค๋ฒ„๋ผ์ด๋”ฉ

  • model
    • ๊ธฐ๋ณธ ๋ทฐ(View, Template, RedirectView)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์ œ๋„ค๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•จ
  • queryset
    • ๊ธฐ๋ณธ ๋ทฐ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์ œ๋„ค๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•จ
    • queryset์„ ์‚ฌ์šฉํ•˜๋ฉด model ์†์„ฑ์€ ๋ฌด์‹œ๋จ
  • template_name
    • TemplateView๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ์ œ๋„ค๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•จ
    • ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ ๋ช…์„ ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ•จ
  • context_object_name
    • ๋ทฐ์—์„œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์— ์ „๋‹ฌํ•˜๋Š” ์ปจํ…์ŠคํŠธ ๋ณ€์ˆ˜๋ช…์„ ์ง€์ •ํ•จ
  • paginate_by
    • ListView์™€ ๋‚ ์งœ ๊ธฐ๋ฐ˜ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•จ
    • ํŽ˜์ด์ง• ๊ธฐ๋Šฅ์ด ํ™œ์„ฑํ™” ๋œ ๊ฒฝ์šฐ, ํŽ˜์ด์ง€๋‹น ์ถœ๋ ฅ ํ•ญ๋ชฉ ์ˆ˜๋ฅผ ์ •์ˆ˜๋กœ ์ง€์ •
  • date_field
    • ๋‚ ์งœ ๊ธฐ๋ฐ˜ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•จ
    • ์ด ํ•„๋“œ์˜ ํƒ€์ž…์€ DateField ๋˜๋Š” DateTimeField ์ž„
  • form_class
    • FormView, CreateView, UpdateView ์—์„œ ํผ์„ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋ฅผ ์ง€์ •
  • success_url
    • FormView, CreateView, UpdateView, DeleteView ์—์„œ ํผ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณตํ•œ ํ›„, ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ํ•  URL ์ฃผ์†Œ

๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ

  • def get_queryset()
    • ๊ธฐ๋ณธ ๋ทฐ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์ œ๋„ค๋ฆญ ๋ทฐ์—์„œ ์‚ฌ์šฉํ•จ
    • ๊ธฐ๋ณธ์œผ๋กœ queryset ์†์„ฑ์„ ๋ฐ˜ํ™˜ํ•จ
    • queryset ์†์„ฑ์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ๋ชจ๋ธ ๋งค๋‹ˆ์ € ํด๋ž˜์Šค์˜ all() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด QuerySet ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•จ
  • def get_context_data(**kwargs)
    • ๋ทฐ์—์„œ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์— ๋„˜๊ฒจ์ฃผ๋Š” ์ปจํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•จ
  • def form_valid(form)

๋ชจ๋ธ์„ ์ง€์ •ํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•

  1. model ์†์„ฑ ๋ณ€์ˆ˜ ์ง€์ •
  2. queryset ์†์„ฑ ๋ณ€์ˆ˜ ์ง€์ •
  3. def get_quesyset() ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ

์˜ˆ์ œ ์ฝ”๋“œ

from django.views.generic import ListView, DetailView
from .models import Question

class IndexView(ListView):
    template_name = 'cbvpolls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        return Question.objects.order_by('-pub_date')[:5]

class DetailView(DetailView):
    model = Question
    template_name = 'cbvpolls/detail.html'

class ResultsView(DetailView):
    model = Question
    template_name = 'cbvpolls/results.html'

์ฐธ๊ณ  ์ž๋ฃŒ

[Built-in class-based generic views Django documentation](https://docs.djangoproject.com/en/5.0/topics/class-based-views/generic-display/)

์žฅ๊ณ  ๋งˆ์Šคํ„ฐํ•˜๊ธฐ - 10์žฅ - generic view - ๊น€๋•ก๋•กโ€™s blog

[Django] ์ œ๋„ค๋ฆญ ๋ทฐ (Generic View)

02) ํด๋ž˜์Šคํ˜• ๋ทฐ (CBV)

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ