Django Generic View
์์ ํํ ๋ฆฌ์ผ์์๋ HttpResonse
, render
๋ฅผ ์ด์ฉํ์ฌ ํจ์ํ ๋ทฐ(Function-Based View) ๋ฅผ ๊ตฌํํ๋ค. ํ์ง๋ง ๊ทธ ๊ณผ์ ์์ ์ฝ๋๊ฐ ๋ฐ๋ณต๋๊ณ , ๊ตฌํ์ด ๋ณต์กํด์ง๋ ๊ฒ์ ๋ณผ ์ ์์๋ค. ํด๋์ค ๋ทฐ(Class-Based View)๊ฐ ํจ์ํ ๋ทฐ์ ๋ฌ๋ฆฌ ์ด๋ป๊ฒ ๊ตฌํ๋๊ณ , ์ด๋ค ์ฅ์ ์ด ์๋์ง ์ดํด ๋ณผ ๊ฒ์ด๋ค.
Function-Based View
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))
๊ฐ์ฅ ๊ธฐ์ด์ ์ธ ๊ตฌํ ๋ฐฉ๋ฒ์ด๋ค.
from django.shortcuts import render
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {
'lastest_question_list': latest_question_list,
}
return render(request, 'polls/index.html', context)
render
๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํํ ๋ฐฉ๋ฒ์ด๋ค. ์์ ๊ตฌํ ๋ฐฉ๋ฒ๋ณด๋ค๋ ๋ ๊ฐ๋จํด์ง๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. template
์ฝ๋ ๋ถ๋ถ์ด ์์ด์ง๊ณ , render๋ก ๋ฐ๋ก ์ ๋ฌํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
Class-based View
Class-Based View ์ฌ์ฉ ๊ฐ์ด๋๋ผ์ธ
๋ทฐ๋ ๊ฐ๋จ ๋ช ๋ฃํด์ผ ํ๋ค.
๋ทฐ ์ฝ๋์ ์์ ์ ์ผ๋ฉด ์ ์์๋ก ์ข๋ค.
๋ทฐ ์์์ ๊ฐ์ ์ฝ๋๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉํ์ง ์๋๋ค.
๋ทฐ๋ ํ๋ ์ ํ ์ด์ ๋ก์ง์์ ๊ด๋ฆฌํ๊ณ ๋น์ฆ๋์ค ๋ก์ง์ ๋ชจ๋ธ์์ ์ฒ๋ฆฌํ๋ค. ๋งค์ฐ ํน๋ณํ ๊ฒฝ์ฐ์๋ง ํผ์์ ์ฒ๋ฆฌํ๋ค.
403, 404, 500 ์๋ฌ ํธ๋ค๋ง์๋ CBV๋ฅผ ์ด์ฉํ์ง ์๊ณ FBV๋ฅผ ์ด์ฉํ๋ค.
๋ฏน์ค์ธ์ ๊ฐ๋จ๋ช ๋ฃํด์ผ ํ๋ค.
Generic Views
์์ ํจ์ํ ๋ทฐ์์ ๋ฐ๋ณต๋๋ ๋ถ๋ถ์ ํจํดํํ์ฌ์ ์ฌ์ฉํ๊ธฐ ์ฝ๊ฒ ์ถ์ํ ํ์๋ค. Generic View๋ ์น ๊ฐ๋ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ธฐ๋ฅ์ ์ฅ๊ณ ์์ ๋ฏธ๋ฆฌ ์ ๊ณตํด์ค๋ค.
from django.views import generic
class IndexView(generic.ListView):
model = Question
์์ ๊ฐ์ด ์ต์ํ ๊ทธ ๋ทฐ๊ฐ ์ด๋ค ๋ชจ๋ธ์ ์ฌ์ฉํ ๊ฒ์ธ์ง๋ง ์ง์ ํด์ฃผ๋ฉด generic view๊ฐ ๋ชจ๋ ๊ฑธ ์์์ ํด์ค๋ค. ํ์์ ๋ฐ๋ผ ๊ฐ ๋ทฐ๋ง๋ค ๋ฌ๋ผ์ง๋ ๊ฐ์ ๋ฃ์ด์ฃผ๊ธฐ๋ง ํ๋ฉด๋๋ค. Class-based View๋ฅผ ์ฌ์ฉํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ฅ์ ์ด ์๋ค.
GET, POST๋ฑ HTTP ๋ฉ์๋์ ๋ฐ๋ฅธ ์ฒ๋ฆฌ ์ฝ๋ ์์ฑ์ ์ฝ๋ ๊ตฌ์กฐ๊ฐ ๊น๋ํด์ง๋ค. ๊ฐ๋ ์ฑ์ด ๋์์ง๋ค.
๋ค์ค์์ ๊ฐ์ ๊ฐ์ฒด์งํฅ ๊ธฐ๋ฒ์ ํ์ฉํด ์ ๋๋ฆญ ๋ทฐ, ๋ฏน์ค์ธ ํด๋์ค ๋ฑ์ ์ฌ์ฉํด ์ฝ๋์ ์ฌ์ฌ์ฉ๊ณผ ๊ฐ๋ฐ ์์ฐ์ฑ์ ๋์ฌ์ค๋ค.
๋ณต์กํ ๊ตฌํ์ ๊ฐ๋ฅํ๊ฒ ํด์ค๋ค.
ํ์ง๋ง ํด๋์คํ ๋ทฐ๊ฐ ํจ์ํ ๋ทฐ๋ฅผ ์์ ํ ๋์ฒดํ์ง๋ ์๋๋ค. ์ํฉ์ ๋ฐ๋ผ ์ ํํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ด์ง ์ด๋ ํ์ชฝ์ด ๋ ์ข๋ค๊ณ ๋งํ๊ธฐ๋ ์ด๋ ต๋ค.
Generic View๋ from django.views import generic
์ธ๋ถ ๋ชจ๋ ์ ์ฉ์ด ํ์ํ๋ฉฐ ๋ค์๊ณผ ๊ฐ์ด ๋ถ๋ฅํ ์ ์๋ค.
Base View : ๋ทฐ ํด๋์ค๋ฅผ ์์ฑํ๊ณ ๋ค๋ฅธ ์ ๋๋ฆญ ๋ทฐ์ ๋ถ๋ชจ ํด๋์ค๊ฐ ๋๋ ๊ธฐ๋ณธ ๋ทฐ
View : ์ต์์ ๋ถ๋ชจ ์ ๋๋ฆญ ๋ทฐ ํด๋์ค
TemplateView : ์ฃผ์ด์ง ํ ํ๋ฆฟ์ผ๋ก ๋ ๋๋ง
RedirectView : ์ฃผ์ด์ง URL๋ก redirect
Genreic Display View : ๊ฐ์ฒด์ ๋ชฉ๋ก ๋๋ ํ๋์ ๊ฐ์ฒด ์์ธ ์ ๋ณด๋ฅผ ๋ณด์ฌ์ฃผ๋ ๋ทฐ
DetailView : ์กฐ๊ฑด์ ๋ง๋ ํ๋์ ๊ฐ์ฒด ์ถ๋ ฅ
ListView : ์กฐ๊ฑด์ ๋ง๋ ๊ฐ์ฒด ๋ชฉ๋ก ์ถ๋ ฅ
Generic Edit View : Form์ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฑ, ์์ , ์ญ์ ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ทฐ
FormView : ํผ์ด ์ฃผ์ด์ง๋ฉด ํด๋น ํผ์ ์ถ๋ ฅ
CreateView : ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํผ ์ถ๋ ฅ
UpdateView : ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์์ ํ๋ ํผ ์ถ๋ ฅ
DeleteView : ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์ญ์ ํ๋ ํผ ์ถ๋ ฅ
Generic Date View : ๋ ์ง ๊ธฐ๋ฐ ๊ฐ์ฒด์ ์ฐ/์/์ผ ํ์ด์ง๋ก ๊ตฌ๋ถํด ๋ณด์ฌ์ฃผ๋ ๋ทฐ
YearArchiveView: ์ฃผ์ด์ง ์ฐ๋์ ํด๋นํ๋ ๊ฐ์ฒด ์ถ๋ ฅ
MonthArchiveView: ์ฃผ์ด์ง ์์ ํด๋นํ๋ ๊ฐ์ฒด ์ถ๋ ฅ
DayArchiveView: ์ฃผ์ด์ง ๋ ์ง์ ํด๋นํ๋ ๊ฐ์ฒด ์ถ๋ ฅ
TodayArchiveView: ์ค๋ ๋ ์ง์ ํด๋นํ๋ ๊ฐ์ฒด ์ถ๋ ฅ
DateDetailView: ์ฃผ์ด์ง ์ฐ, ์, ์ผ PK(๋๋ ์ฌ๋ฌ๊ทธ)์ ํด๋นํ๋ ๊ฐ์ฒด ์ถ๋ ฅ
์์
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic
# from django.template import loader
# from django.http import Http404
from .models import Question
# Create your views here.
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
return Question.objects.order_by('-pub_date')[:6]
"""๊ธฐ๋ณธ ๋ทฐ(View, Template, RedirectView) 3๊ฐ๋ฅผ ์ ์ธํ๊ณ ๋ชจ๋ ์ ๋๋ฆญ ๋ทฐ์์ ์ฌ์ฉํ๋ค.
๋ํดํธ๋ queryset ์์ฑ์ ๋ฐํํ๋ค.
queryset ์์ฑ์ด ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ๋ชจ๋ธ ๋งค๋์ ํด๋์ค์ all() ๋ฉ์๋๋ฅผ ํธ ์ถํด QuerySet ๊ฐ์ฒด๋ฅผ ์์ฑํด ๋ฐํํ๋ค."""
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, question_id):
question = get_object_or_404(Question, pk = question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {'question' : question, 'error_message':"You didn't select a choice"})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
์ฐธ๊ณ
Last updated
Was this helpful?