Commit 5410d2b1 authored by Joei Yucoco's avatar Joei Yucoco

Converted FBVs to CBVs

- AuthorAddView and BookAddView were converted into CreateViews
parent fb4cf22b
...@@ -8,8 +8,8 @@ urlpatterns = [ ...@@ -8,8 +8,8 @@ urlpatterns = [
path('books/<int:pk>/details/', BookDetailsView.as_view(), name='books-detail'), path('books/<int:pk>/details/', BookDetailsView.as_view(), name='books-detail'),
path('authors/', AuthorsView.as_view(), name='authors-list'), path('authors/', AuthorsView.as_view(), name='authors-list'),
path('authors/<int:pk>/details/', AuthorDetailsView.as_view(), name='authors-detail'), path('authors/<int:pk>/details/', AuthorDetailsView.as_view(), name='authors-detail'),
path('authors/add/', AuthorAddView, name='authors-add'), path('authors/add/', AuthorAddView.as_view(), name='authors-add'),
path('books/add/', BookAddView, name='books-add'), path('books/add/', BookAddView.as_view(), name='books-add'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.views import View from django.views import View
from django.urls import reverse
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView
from .forms import AuthorForm, BooksForm from .forms import AuthorForm, BooksForm
from .models import Author, Books from .models import Author, Books
...@@ -32,26 +34,19 @@ class AuthorDetailsView(DetailView): ...@@ -32,26 +34,19 @@ class AuthorDetailsView(DetailView):
template_name = 'bookshelf/author_details.html' template_name = 'bookshelf/author_details.html'
#TODO: add template, app urls, class AuthorAddView(CreateView):
def AuthorAddView(request): model = Author
if request.method == 'POST': form_class = AuthorForm
form = AuthorForm(request.POST) template_name = 'bookshelf/author_add.html'
if form.is_valid():
new_author = form.save()
return redirect('bookshelf:authors-detail', pk=new_author.pk)
else:
form = AuthorForm()
return render(request, 'bookshelf/author_add.html', {'form': form})
def get_success_url(self):
return reverse('bookshelf:authors-detail', kwargs={'pk': self.object.pk})
def BookAddView(request): class BookAddView(CreateView):
if request.method == 'POST': model = Books
form = BooksForm(request.POST) form_class = BooksForm
if form.is_valid(): template_name = 'bookshelf/book_add.html'
new_book = form.save()
return redirect('bookshelf:books-detail', pk=new_book.pk)
else: def get_success_url(self):
form = BooksForm() return reverse('bookshelf:books-detail', kwargs={'pk': self.object.pk})
return render(request, 'bookshelf/book_add.html', {'form': form})
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