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