implemented edit-book and edit-author pages complete with redirect to updated detailsview

parent 71da1974
......@@ -13,6 +13,9 @@ class Author(models.Model):
def get_absolute_url(self):
return reverse('bookshelf:author-detail', kwargs={'pk': self.pk})
def get_edit_url(self):
return reverse('bookshelf:edit-author', kwargs={'pk': self.pk})
class Book(models.Model):
......@@ -27,4 +30,7 @@ class Book(models.Model):
return self.title
def get_absolute_url(self):
return reverse('bookshelf:book-detail', kwargs={'pk': self.pk})
return reverse('bookshelf:book-detail', kwargs={'pk': self.pk})
def get_edit_url(self):
return reverse('bookshelf:edit-book', kwargs={'pk': self.pk})
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block title %}Add New Author{% endblock %}
{% block content %}
<form action="http://localhost:8000/authors/add" method="post">
{% csrf_token %}
......
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block title %}Add New Book{% endblock %}
{% block content %}
<form action="http://localhost:8000/books/add" method="post">
{% csrf_token %}
......
......@@ -13,6 +13,7 @@
<li><a href="{{ book.get_absolute_url }}">{{ book }}</a></li>
{% endfor %}
<br></ul>
<button type="button"><a href="{{ author.get_edit_url }}">Edit Author</a></button><br>
<a href="http://localhost:8000/home/">Home</a>
<a href="http://localhost:8000/books/">Books</a>
<a href="http://localhost:8000/authors/">Authors</a>
......
......@@ -9,6 +9,7 @@
<p>{{ book.year_published }}<br></p>
<p>{{ book.ISBN }}<br></p>
<p>{{ book.blurb }}<br><br></p>
<button type="button"><a href="{{ book.get_edit_url }}">Edit Book</a></button><br>
<a href="http://localhost:8000/home/">Home</a>
<a href="http://localhost:8000/books/">Books</a>
<a href="http://localhost:8000/authors/">Authors</a>
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Author{% endblock %}
{% block content %}
<form action="{{ author.get_edit_url }}" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }}
{{ field }}<br>
{% endfor %}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Book{% endblock %}
{% block content %}
<form action="{{ book.get_edit_url }}" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }}
{{ field }}<br>
{% endfor %}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path
from .views import homepage, BooksView, BookDetailView, AuthorsView, AuthorDetailView, AddNewBookView, AddNewAuthorView
from .views import homepage, BooksView, BookDetailView, AuthorsView, AuthorDetailView, AddNewBookView, AddNewAuthorView, EditBookPageView, EditAuthorPageView
urlpatterns = [
path('home/', homepage, name='homepage'),
path('books/', BooksView.as_view(), name='book-list'),
path('books/add', AddNewBookView.as_view(), name='add-book'),
path('books/<int:pk>/edit', EditBookPageView.as_view(), name='edit-book'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'),
path('authors/', AuthorsView.as_view(), name='author-list'),
path('authors/add', AddNewAuthorView.as_view(), name='add-author'),
path('authors/<int:pk>/edit', EditAuthorPageView.as_view(), name='edit-author'),
path('authors/<int:pk>/details', AuthorDetailView.as_view(), name='author-detail'),
]
......
......@@ -23,7 +23,7 @@ class AuthorsView(ListView):
def get(self, request):
authors = Author.objects.all()
return render(request, 'bookshelf/authors.html', {'authors': authors})
class AuthorDetailView(DetailView):
def get(self, request, pk):
specificauthor = Author.objects.get(pk=pk)
......@@ -43,7 +43,6 @@ class AddNewBookView(DetailView):
title = request.POST.get('title')
author = postedauthor
print(author)
publisher = request.POST.get('publisher')
year_published = request.POST.get('year_published')
ISBN = request.POST.get('ISBN')
......@@ -85,4 +84,66 @@ class AddNewAuthorView(DetailView):
if form.is_valid():
return render(request, 'bookshelf/author_details.html', {'author': newauthor})
else:
return self.get(request, *args, **kwargs)
\ No newline at end of file
return self.get(request, *args, **kwargs)
class EditBookPageView(DetailView):
model = Book
def get(self, request, pk):
form = BookForm
currentbook = Book.objects.get(pk=pk)
return render(request, 'bookshelf/edit-book.html', {'form':form, 'book':currentbook})
def post(self, request, pk):
form = BookForm(request.POST)
postedauthor = Author.objects.get(pk=request.POST.get('author'))
title = request.POST.get('title')
author = postedauthor
publisher = request.POST.get('publisher')
year_published = request.POST.get('year_published')
ISBN = request.POST.get('ISBN')
blurb = request.POST.get('blurb')
Book.objects.get(pk=pk).delete()
editedbook = Book(title=title, author=author, publisher=publisher, year_published=year_published, ISBN=ISBN, blurb=blurb)
editedbook.save()
updatedbookpk = editedbook.pk
updatedbook = Book.objects.get(pk=updatedbookpk)
if form.is_valid():
return render(request, 'bookshelf/book_details.html', {'book': updatedbook})
else:
return self.get(request, updatedbookpk)
class EditAuthorPageView(DetailView):
model = Author
def get(self, request, pk):
form = AuthorForm()
currentauthor = Author.objects.get(pk=pk)
return render(request, 'bookshelf/edit-author.html', {'form':form, 'author':currentauthor})
def post(self, request, pk):
form = AuthorForm(request.POST)
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
age = request.POST.get('age')
nationality = request.POST.get('nationality')
bio = request.POST.get('bio')
Author.objects.get(pk=pk).delete()
editedauthor = Author(first_name=first_name, last_name=last_name, age=age, nationality=nationality, bio=bio)
editedauthor.save()
updatedauthorpk = editedauthor.pk
updatedauthor = Author.objects.get(pk=updatedauthorpk)
if form.is_valid():
return render(request, 'bookshelf/author_details.html', {'author': updatedauthor})
else:
return self.get(request, updatedauthorpk)
\ 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