Added imports and some classes for add and edit

parent 45c7a902
from django.shortcuts import render
from django.http import HttpResponse, Http404
from django.shortcuts import render, reverse
from django.http import Http404
from .models import Author, Book
from django.views import View
from .forms import BookForm, AuthorForm
from django.shortcuts import get_object_or_404
from django.views.generic.edit import UpdateView
def home(request):
return render(request, "bookshelf/home.html")
......@@ -35,4 +39,50 @@ class AuthorsDetailView(View):
except Author.DoesNotExist:
raise Http404("Author does not exist!")
books_list= Book.objects.all().filter(author=author)
return render(request, "bookshelf/author_detail.html", {"author":author,"books_list":books_list})
\ No newline at end of file
return render(request, "bookshelf/author_detail.html", {"author":author,"books_list":books_list})
class AddBookView(View):
model = Book
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['form'] = BookForm()
return context
def post(self, request,*args,**kwargs):
form = BookForm(request.POST)
if form.is_valid():
new_book = form.save()
return render(request, "bookshelf/book_detail.html", {'book': new_book})
else:
return render(request, "bookshelf/add-book.html", {'form': form})
class AddAuthorView(View):
model = Author
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['form'] = AuthorForm()
return context
def post(self, request,*args,**kwargs):
form = AuthorForm(request.POST)
if form.is_valid():
new_author = form.save()
return render(request, "bookshelf/author_detail.html", {'author': new_author})
else:
return render(request, "bookshelf/add-author.html", {'form': form})
class BookEditView(UpdateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
def get_success_url(self):
return reverse('bookshelf:book_detail', kwargs={'book_id':self.object.pk})
class AuthorEditView(UpdateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
def get_success_url(self):
return reverse('bookshelf:author_detail', kwargs={'author_id':self.object.pk})
\ 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