๐DetailView
- ๋ฐ์ดํฐ ๋จ์ผ ๊ฑด ์กฐํ๋ฅผ ์ํ ๋ทฐ ํด๋์ค
- ๋ทฐ๊ฐ ์คํ๋๋ ๋์ self.object์๋ ๋ทฐ๊ฐ ์๋ํ๋ ๊ฐ์ฒด๊ฐ ํฌํจ๋๋ค.
pk_url_kwarg
ํ๋์ Path Variable(Parameter) ๋ช
์ ๋ฃ์ผ๋ฉด ์์์ self.object์ ์กฐํ๋ ๋ฐ์ดํฐ๋ฅผ ํ ๋นํด์ค
Detail ๋ทฐ ํด๋์ค์ ๊ธฐ๋ณธ ํํ
class DetailView:
model: Model
template_name: str
pk_url_kwarg: str
def get_context_data(self, **kwargs):
return response: dict
from django.utils import timezone
from django.views.generic.detail import DetailView
from articles.models import Article
class ArticleDetailView(DetailView):
model = Article
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["now"] = timezone.now()
return context
๐์ ์ฉ
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 TaskDetailView(DetailView):
model = Task
template_name = "pages/task_detail.html"
pk_url_kwarg = 'task_id'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["checklists"] = ChecklistItem.objects.filter(task=self.object).all()
return context
<div class="row justify-content-center">
<div class="col-12">
<h4 class="mt-2 p-2">
<span class="badge badge-info">
{{object.get_type_display}}
</span>
{{object.title}}
</h4>
<dl class="row mt-4 p-2">
<dt class="col-sm-2">๋ง๊ฐ์ผ</dt>
<dd class="col-sm-10">{{object.due|date:"Y๋
m์ d์ผ H์ i๋ถ"}}</dd>
<dt class="col-sm-2">์์ฑ์ผ</dt>
<dd class="col-sm-10">{{object.created_at|date:"Y๋
m์ d์ผ H์ i๋ถ"}}</dd>
</dl>
</div>
</div>
๋ชจ๋ธ์ ์์ฑ๊ฐ์ ์ฌ๋์ด ์ฝ์ ์ ์๋ ๊ฐ์ผ๋ก ๋ณํํ๊ธฐ
์ฐธ๊ณ ์๋ฃ1 - Django extra instance methods
์ฐธ๊ณ ์๋ฃ2 - StackOverflow