Commit 435723a9 authored by Brendan Fausto's avatar Brendan Fausto

Added UpdateView for Books and Authors, and respective templates

parent 54404855
......@@ -8,6 +8,6 @@ class BookForm(ModelForm):
class AuthorForm(ModelForm):
class Meta:
model = Books
model = Authors
fields = ['first_name','last_name','age','nationality','bio']
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}
Edit Author
{% endblock %}
{% block header %}
<h1>Edit Author</h1>
{% endblock %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form method = "POST">
{% csrf_token %}
{{ form.as_p }}
<input type = "submit" value = "Save Changes">
</form>
<p>
<a href = "http://127.0.0.1:8000/home">Home</a>
<a href = "http://127.0.0.1:8000/authors">Authors</a>
<a href = "http://127.0.0.1:8000/books">Books</a>
</p>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}
Edit Book
{% endblock %}
{% block header %}
<h1>Edit Book</h1>
{% endblock %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form method = "POST">
{% csrf_token %}
{{ form.as_p }}
<input type = "submit" value = "Save Changes">
</form>
<p>
<a href = "http://127.0.0.1:8000/home">Home</a>
<a href = "http://127.0.0.1:8000/authors">Authors</a>
<a href = "http://127.0.0.1:8000/books">Books</a>
</p>
{% endblock %}
\ No newline at end of file
# about/urls.py
from django.urls import path
from .views import (home_view, AuthorListView, BookListView, BookDetailView, AuthorDetailView, BookCreateView, AuthorCreateView)
from .views import (home_view, AuthorListView, BookListView, BookDetailView, AuthorDetailView,
BookCreateView, AuthorCreateView, BookUpdateView, AuthorUpdateView)
urlpatterns = [
path('home', home_view, name='index'),
......@@ -11,6 +12,8 @@ urlpatterns = [
path('authors/<int:pk>/details', AuthorDetailView.as_view(), name='authors-details'),
path('books/add', BookCreateView.as_view(), name='books-create'),
path('authors/add', AuthorCreateView.as_view(), name='authors-create'),
path('books/<int:pk>/edit', BookUpdateView.as_view(), name='books-update'),
path('authors/<int:pk>/edit', AuthorUpdateView.as_view(), name='author-update'),
]
app_name = "bookshelf" # for proper namespacing in urls, otherwise will result in errors
\ No newline at end of file
......@@ -2,16 +2,20 @@ from django.shortcuts import render
from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import UpdateView
from django.shortcuts import get_object_or_404
from django.views.generic.edit import CreateView, UpdateView
from .models import Books, Authors
from .forms import BookForm, AuthorForm
def home_view(request):
return render(request, 'bookshelf/homepage.html', {
'page_title': "Books and Authors"
})
class AuthorListView(ListView):
def get(self, request):
authors = Authors.objects.all()
......@@ -19,6 +23,7 @@ class AuthorListView(ListView):
'authors': authors
})
class BookListView(ListView):
def get(self, request):
books = Books.objects.all()
......@@ -26,6 +31,7 @@ class BookListView(ListView):
'books': books
})
class BookDetailView(DetailView):
model = Books
template_name = 'bookshelf/books_details.html'
......@@ -34,17 +40,36 @@ class BookDetailView(DetailView):
print("BooksDetailView is being called")
return super().get(request, *args, **kwargs)
class AuthorDetailView(DetailView):
model = Authors
template_name = 'bookshelf/authors_details.html'
class BookCreateView(CreateView):
model = Books
fields = '__all__'
template_name = 'add-book.html'
template_name = 'bookshelf/add-book.html'
class AuthorCreateView(CreateView):
model = Books
model = Authors
fields = '__all__'
template_name = 'add-book.html'
template_name = 'bookshelf/add-author.html'
class BookUpdateView(UpdateView):
model = Books
form_class = BookForm
template_name = 'bookshelf/edit-book.html'
def get_success_url(self):
return reverse_lazy('books_details', kwargs={'pk': self.object.pk})
class AuthorUpdateView(UpdateView):
model = Authors
form_class = AuthorEditForm
template_name = 'bookshelf/edit-author.html'
def get_success_url(self):
return reverse_lazy('author_details', kwargs={'pk': self.object.pk})
# Create your views here.
No preview for this file type
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