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 ์„ค์ • ๊ฐ€๋Šฅ  
โš ๏ธ **GitHub.com Fallback** โš ๏ธ