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

parent 71da1974
...@@ -14,6 +14,9 @@ class Author(models.Model): ...@@ -14,6 +14,9 @@ class Author(models.Model):
def get_absolute_url(self): def get_absolute_url(self):
return reverse('bookshelf:author-detail', kwargs={'pk': self.pk}) 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): class Book(models.Model):
title = models.CharField(max_length=180) title = models.CharField(max_length=180)
...@@ -28,3 +31,6 @@ class Book(models.Model): ...@@ -28,3 +31,6 @@ class Book(models.Model):
def get_absolute_url(self): 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' %} {% extends 'base.html' %}
{% load static %} {% load static %}
{% block title %}My Favorite Books{% endblock %} {% block title %}Add New Author{% endblock %}
{% block content %} {% block content %}
<form action="http://localhost:8000/authors/add" method="post"> <form action="http://localhost:8000/authors/add" method="post">
{% csrf_token %} {% csrf_token %}
......
{% extends 'base.html' %} {% extends 'base.html' %}
{% load static %} {% load static %}
{% block title %}My Favorite Books{% endblock %} {% block title %}Add New Book{% endblock %}
{% block content %} {% block content %}
<form action="http://localhost:8000/books/add" method="post"> <form action="http://localhost:8000/books/add" method="post">
{% csrf_token %} {% csrf_token %}
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
<li><a href="{{ book.get_absolute_url }}">{{ book }}</a></li> <li><a href="{{ book.get_absolute_url }}">{{ book }}</a></li>
{% endfor %} {% endfor %}
<br></ul> <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/home/">Home</a>
<a href="http://localhost:8000/books/">Books</a> <a href="http://localhost:8000/books/">Books</a>
<a href="http://localhost:8000/authors/">Authors</a> <a href="http://localhost:8000/authors/">Authors</a>
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<p>{{ book.year_published }}<br></p> <p>{{ book.year_published }}<br></p>
<p>{{ book.ISBN }}<br></p> <p>{{ book.ISBN }}<br></p>
<p>{{ book.blurb }}<br><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/home/">Home</a>
<a href="http://localhost:8000/books/">Books</a> <a href="http://localhost:8000/books/">Books</a>
<a href="http://localhost:8000/authors/">Authors</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 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 = [ urlpatterns = [
path('home/', homepage, name='homepage'), path('home/', homepage, name='homepage'),
path('books/', BooksView.as_view(), name='book-list'), path('books/', BooksView.as_view(), name='book-list'),
path('books/add', AddNewBookView.as_view(), name='add-book'), 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('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'),
path('authors/', AuthorsView.as_view(), name='author-list'), path('authors/', AuthorsView.as_view(), name='author-list'),
path('authors/add', AddNewAuthorView.as_view(), name='add-author'), 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'), path('authors/<int:pk>/details', AuthorDetailView.as_view(), name='author-detail'),
] ]
......
...@@ -43,7 +43,6 @@ class AddNewBookView(DetailView): ...@@ -43,7 +43,6 @@ class AddNewBookView(DetailView):
title = request.POST.get('title') title = request.POST.get('title')
author = postedauthor author = postedauthor
print(author)
publisher = request.POST.get('publisher') publisher = request.POST.get('publisher')
year_published = request.POST.get('year_published') year_published = request.POST.get('year_published')
ISBN = request.POST.get('ISBN') ISBN = request.POST.get('ISBN')
...@@ -86,3 +85,65 @@ class AddNewAuthorView(DetailView): ...@@ -86,3 +85,65 @@ class AddNewAuthorView(DetailView):
return render(request, 'bookshelf/author_details.html', {'author': newauthor}) return render(request, 'bookshelf/author_details.html', {'author': newauthor})
else: else:
return self.get(request, *args, **kwargs) 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