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

Now, FBV is converted to CBV

parent 4a9b9898
from django.urls import path from django.urls import path
from . import views from .views import HomeView, AuthorListView, AuthorDetailView, BookListView, BookDetailView
urlpatterns = [ urlpatterns = [
path('home/', views.home, name = "home"), # for Home (landing page)
path('books/', views.books, name = "books"), path('', HomeView.as_view(), name='home'),
path('books/<int:pk>/details', views.books_detail, name = "books_detail"), # for Authors page
path('authors/', views.authors, name = "authors"), path('authors/', AuthorListView.as_view(), name='authors'),
path('authors/<int:pk>/details', views.authors_detail, name = "authors_detail"), 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 django.views import View
from .models import Author, Books from django.views.generic import ListView, DetailView
from datetime import date
from django.template import loader
from django.shortcuts import render 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) # Views for Authors page
def home(request): class AuthorListView(ListView):
context = {} model = Author
template = loader.get_template('home/index.html') template_name = 'authors/index.html'
return HttpResponse(template.render(context, request)) context_object_name = 'authors'
ordering = ['first_name']
# for Authors page class AuthorDetailView(DetailView):
def authors(request): model = Author
authors = Author.objects.order_by('first_name') template_name = 'authors/author.html'
context = {'authors': authors} context_object_name = 'author'
template = loader.get_template('authors/index.html')
return HttpResponse(template.render(context, request))
def authors_detail(request, pk): # this line for getting the param value "pk"
author = Author.objects.get(pk=pk) # returns Dict[str, Any]
# get books related to the author's id. def get_context_data(self, **kwargs):
books = Books.objects.filter(author__pk = pk) context = super().get_context_data(**kwargs)
context = {'author': author, 'books': books} # get by self == Author's id to get Book's foreign key that matches up.
template = loader.get_template('authors/author.html') context['books'] = Books.objects.filter(author = self.object)
return HttpResponse(template.render(context, request)) return context
# for Books page # Views for Books page
def books(request): class BookListView(ListView):
books = Books.objects.order_by('year_published') model = Books
context = {'books': books} template_name = 'books/index.html'
template = loader.get_template('books/index.html') context_object_name = 'books'
return HttpResponse(template.render(context, request)) ordering = ['year_published']
def books_detail(request, pk): class BookDetailView(DetailView):
book = Books.objects.get(pk=pk) model = Books
context = {'book': book} template_name = 'books/book.html'
template = loader.get_template('books/book.html') context_object_name = 'book'
return HttpResponse(template.render(context, request)) \ No newline at end of file
\ 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