13. Built in class based generic views - chohankyun/python-django-study GitHub Wiki
##Built-in class-based generic views
- ๋ฏธ๋ฆฌ ๋ง๋ค์ด ๋์ ํด๋์ค
- Django ๋ model layer ์ template layer ์์ ๋จ์ ๋ฐ๋ณต ์ ๊ฑฐ
- generic views ๋ views layer ์์ ๋จ์ ๋ฐ๋ณต ์ ๊ฑฐ
##Built-in class-based generic views ๊ธฐ๋ฅ
- list(์ฌ๋ฌ๊ฐ์ ๊ฐ์ฒด) view, detail(ํ๋์ ๊ฐ์ฒด) view ์ ๊ณต
- ๊ฐ์ฒด์ ์์ฑ, ๋ณ๊ฒฝ, ์ญ์ ๊ธฐ๋ฅ ์ ๊ณต
- date-based objects (๋ /์/์ผ) ์ ๊ณต
##Built-in class-based generic views ํ์ฅ
- generic views ์ ์์ ํ ์์ฑ ์ค๋ฒ๋ผ์ด๋, ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋
- generic views ์ฌ์ฉ์ด ์ด๋ ค์ธ ์, ๊ทธ๋ฅ ์์ ์ ํด๋์ค, ๋๋ ํจ์ ๋ทฐ ๋ง๋ฌ(์ด ๋ถ๋ถ์ ์ข ๊ทธ๋ฌ๋ค..)
##Built-in class-based generic views ์ ๊ฐ์ฒด ํธ์ถ
generic views ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ดํฐ์ ์ฐ๋ํ๋๋ฐ ์์ฃผ ์ ์ฉํจ
###books/models.py
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
class Meta:
ordering = ["-name"]
def __str__(self): # __unicode__ on Python 2
return self.name
###books/urls.py
# Publisher list ํธ์ถ
from django.conf.urls import url
from books.views import PublisherList
urlpatterns = [
url(r'^publishers/$', PublisherList.as_view()),
]
###books/views.py
# Publisher list ํธ์ถ
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
model = Publisher
- default html name : publisher_list.html
- settings.py (APP_DIRS = True) : project/books/templates/books/publisher_list.html
###books/templates/books/publisher_list.html
{% block content %}
<h2>Publishers</h2>
<ul>
{% for publisher in object_list %}
<li>{{ publisher.name }}</li>
{% endfor %}
</ul>
{% endblock %}
- first default ๊ฐ์ฒด ๋ฆฌ์คํธ : object_list
- second default ๊ฐ์ฒด ๋ฆฌ์คํธ : publisher_list
object_list ๋๋ publisher_list ๋ก ์ ๊ทผ ๊ฐ๋ฅ
##customize ๊ฐ์ฒด ์ customize ํ๋ฉด
- customize ๊ฐ์ฒด : context_object_name ์ฌ์ฉ
- customize ํ๋ฉด : template_name ์ฌ์ฉ
###books/views.py
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
model = Publisher
context_object_name = 'my_publisher_s'
template_name = 'books/my_publisher_list.html'
##๊ธฐ์กด ๋ฐ์ดํฐ์ ๋ค๋ฅธ ๋ฐ์ดํฐ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ
DetailView ๋ ํ๋์ ๊ฐ์ฒด๋ง ํธ์ถ
DetailView ์ ๋ฆฌ์คํธ ๊ฐ์ฒด ์ถ๊ฐ ํ๊ธฐ
get_context_data ์ค๋ฒ๋ผ์ด๋
context dictionary ์ ๋ฐ์ดํฐ ์ถ๊ฐ
from django.views.generic import DetailView
from books.models import Publisher, Book
class PublisherDetail(DetailView):
model = Publisher
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(PublisherDetail, self).get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['book_list'] = Book.objects.all()
return context
####model = Publisher ๋ queryset = Publisher.objects.all() ์ ๊ฐ๋ค.
####model = Publisher ์์ผ๋ฉด queryset = Publisher.objects.all() ์๋ต
####queryset = Publisher.objects.all() ์์ผ๋ฉด model = Publisher ์๋ต
from django.views.generic import ListView
from books.models import Publisher
class PublisherList(ListView):
# model = Publisher
queryset = Publisher.objects.all()
context_object_name = 'my_publisher_s'
template_name = 'books/my_publisher_list.html'
##order_by() ๋ฆฌ์คํธ ์์
from django.views.generic import ListView
from books.models import Book
class BookList(ListView):
queryset = Book.objects.order_by('-publication_date')
context_object_name = 'book_list'
##filter() ์กฐ๊ฑด
from django.views.generic import ListView
from books.models import Book
class AcmeBookList(ListView):
context_object_name = 'book_list'
queryset = Book.objects.filter(name='ACME Publishing')
template_name = 'books/acme_list.html'
##๋์ flitering
- urls.py : ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์ ์ ์๋๋ก ์ค์
- views.py : *args ๋๋ **kwargs ์์ ํ๋ผ๋ฏธํฐ ์ถ์ถ
- views.py : ์ถ์ถํ ํ๋ผ๋ฏธํฐ๋ฅผ filter() ์ ์กฐ๊ฑด์ ์ถ๊ฐ
###books/urls.py
url pattern ์ ์ ๊ท์์ฌ์ฉ ํจํด
from django.conf.urls import url
from books.views import PublisherBookList
urlpatterns = [
url(r'^books/([\w-]+)/$', PublisherBookList.as_view()),
]
###books/views.py
get_queryset() ์ค๋ฒ๋ผ์ด๋
self.args ์์ ๊ฐ ์ถ์ถ
from django.shortcuts import get_object_or_404
from django.views.generic import ListView
from books.models import Book, Publisher
class PublisherBookList(ListView):
template_name = 'books/books_by_publisher.html'
def get_queryset(self):
self.publisher = get_object_or_404(Publisher, name=self.args[0])
return Book.objects.filter(publisher=self.publisher)
##kwargs ์ฌ์ฉ ์ถ์ถ
์ค์ ์์๋ args ๋ณด๋ค๋ kwargs ๋ฅผ ๋ ๋ง์ด ์ฌ์ฉ
###books/urls.py
url pattern ์ ์ ๊ท์์ฌ์ฉ ํจํด
from django.conf.urls import url
from books.views import PublisherBookList
urlpatterns = [
url(r'^books/(?P<name>[\w-]+)/$', PublisherBookList.as_view()),
]
###books/views.py self.kwargs ์์ ํ๋ผ๋ฏธํฐ ๊ฐ ์ถ์ถ
from django.views.generic import ListView
from books.models import Book, Publisher
class PublisherBookList(ListView):
template_name = 'books/books_by_publisher.html'
def get_queryset(self):
return Book.objects.filter(name=self.kwargs['name'])
##๊ฐ์ฒด ๊ฐ ์ค ํ๋ ๋ณ๊ฒฝ
get_object() ์ค๋ฒ๋ผ์ด๋
from django.views.generic import DetailView
from django.utils import timezone
from books.models import Author
class AuthorDetailView(DetailView):
queryset = Author.objects.all()
def get_object(self):
# Call the superclass
object = super(AuthorDetailView, self).get_object()
# Record the last accessed date
object.last_accessed = timezone.now()
object.save()
# Return the object
return object
##customize pk ๋ช
pk_url_kwarg ์ default : pk
pk_url_kwarg = primary_key ์ค์ ๊ฐ๋ฅ