Commit 64bd34c5 authored by Deokhyun Lee's avatar Deokhyun Lee

Now, FBV is converted to CBV

parent 4a9b9898
from django.urls import path
from . import views
from .views import HomeView, AuthorListView, AuthorDetailView, BookListView, BookDetailView
urlpatterns = [
path('home/', views.home, name = "home"),
path('books/', views.books, name = "books"),
path('books/<int:pk>/details', views.books_detail, name = "books_detail"),
path('authors/', views.authors, name = "authors"),
path('authors/<int:pk>/details', views.authors_detail, name = "authors_detail"),
# for Home (landing page)
path('', HomeView.as_view(), name='home'),
# for Authors page
path('authors/', AuthorListView.as_view(), name='authors'),
path('authors/<int:pk>/details', AuthorDetailView.as_view(), name='author-detail'),
# for Books page
path('books/', BookListView.as_view(), name='books'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'),
]
\ No newline at end of file
from django.http import HttpResponse
from .models import Author, Books
from datetime import date
from django.template import loader
from django.views import View
from django.views.generic import ListView, DetailView
from django.shortcuts import render
from .models import Author, Books
# View for Home (landing page)
class HomeView(View):
def get(self, request):
return render(request, 'home/index.html')
# views for Home (landing page)
def home(request):
context = {}
template = loader.get_template('home/index.html')
return HttpResponse(template.render(context, request))
# Views for Authors page
class AuthorListView(ListView):
model = Author
template_name = 'authors/index.html'
context_object_name = 'authors'
ordering = ['first_name']
# for Authors page
def authors(request):
authors = Author.objects.order_by('first_name')
context = {'authors': authors}
template = loader.get_template('authors/index.html')
return HttpResponse(template.render(context, request))
class AuthorDetailView(DetailView):
model = Author
template_name = 'authors/author.html'
context_object_name = 'author'
def authors_detail(request, pk):
author = Author.objects.get(pk=pk)
# get books related to the author's id.
books = Books.objects.filter(author__pk = pk)
context = {'author': author, 'books': books}
template = loader.get_template('authors/author.html')
return HttpResponse(template.render(context, request))
# this line for getting the param value "pk"
# returns Dict[str, Any]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# get by self == Author's id to get Book's foreign key that matches up.
context['books'] = Books.objects.filter(author = self.object)
return context
# for Books page
def books(request):
books = Books.objects.order_by('year_published')
context = {'books': books}
template = loader.get_template('books/index.html')
return HttpResponse(template.render(context, request))
# Views for Books page
class BookListView(ListView):
model = Books
template_name = 'books/index.html'
context_object_name = 'books'
ordering = ['year_published']
def books_detail(request, pk):
book = Books.objects.get(pk=pk)
context = {'book': book}
template = loader.get_template('books/book.html')
return HttpResponse(template.render(context, request))
\ No newline at end of file
class BookDetailView(DetailView):
model = Books
template_name = 'books/book.html'
context_object_name = 'book'
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment