Commit 1d05afbd authored by Alliyah Marcelo's avatar Alliyah Marcelo

Implemented Add New Book and Add New Author pages.

parent 28bfb399
from django import forms
from .models import Author, Books
class BooksForm(forms.ModelForm):
class Meta:
model = Books
fields = [
'title',
'author',
'publisher',
'year_published',
'ISBN',
'blurb'
]
class AuthorForm(forms.Form):
class Meta:
model = Author
fields = [
'first_name',
'last_name',
'age',
'nationality',
'bio'
]
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Author{% endblock %}
{% block content %}
<form action="/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Author">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Book{% endblock %}
{% block content %}
<form action="/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
<br> <br>
{{ object.bio }} {{ object.bio }}
<br><br> <br><br>
<a href="{{ AuthorUpdateView.get_absolute_url }}">
<input type="submit" value="Edit Author">
</a>
<br><br>
Books by {{ object }} I love: Books by {{ object }} I love:
<br> <br>
{% for book in books %} {% for book in books %}
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Author{% endblock %}
{% block content %}
<form action="/" method="post">
{% csrf_token %}
{{ form.as_p }}
<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="/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
...@@ -10,5 +10,7 @@ Alliyah's preferred genre when reading books usually centers around dark themes- ...@@ -10,5 +10,7 @@ Alliyah's preferred genre when reading books usually centers around dark themes-
Accordingly, some notable authors she likes are Fyodor Dostoevsky and F.H. Batacan. Accordingly, some notable authors she likes are Fyodor Dostoevsky and F.H. Batacan.
<br> <br>
<br> <br>
<a href="/bookshelf/books/">Books</a> <a href="/bookshelf/authors/">Authors</a> &emsp;<a href="/bookshelf/books/">Books</a>&emsp;&emsp;&ensp;<a href="/bookshelf/authors/">Authors</a>
<br>
&nbsp;<a href="/bookshelf/books/add/">Add Book</a>&emsp;<a href="/bookshelf/authors/add/">Add Author</a>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import home_view, BooksListView, BooksDetailsView, AuthorListView, AuthorDetailsView from .views import home_view, BooksListView, BooksDetailsView, BooksCreateView, BooksUpdateView, AuthorListView, AuthorDetailsView, AuthorCreateView, AuthorUpdateView
urlpatterns = [ urlpatterns = [
...@@ -9,6 +9,10 @@ urlpatterns = [ ...@@ -9,6 +9,10 @@ urlpatterns = [
path('books/<int:pk>/details/', BooksDetailsView.as_view(), name='book-detail'), path('books/<int:pk>/details/', BooksDetailsView.as_view(), name='book-detail'),
path('authors/', AuthorListView.as_view(), name='authors'), path('authors/', AuthorListView.as_view(), name='authors'),
path('author/<int:pk>/details/', AuthorDetailsView.as_view(), name='author-detail'), path('author/<int:pk>/details/', AuthorDetailsView.as_view(), name='author-detail'),
path('books/add/', BooksCreateView.as_view(), name='add-book'),
path('books/<int:pk>/edit', BooksUpdateView.as_view(), name='edit-book'),
path('authors/add/', AuthorCreateView.as_view(), name='add-author'),
path('authors/<int:pk>/edit', AuthorUpdateView.as_view(), name='edit-author'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render, redirect
from django.views import View from django.views import View
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Books from .models import Author, Books
from .forms import BooksForm, AuthorForm
def home_view(request): def home_view(request):
return render(request, 'bookshelf/home.html', {'name': 'Alliyah'}) return render(request, 'bookshelf/home.html', {'name': 'Alliyah'})
...@@ -19,6 +21,18 @@ class BooksDetailsView(DetailView): ...@@ -19,6 +21,18 @@ class BooksDetailsView(DetailView):
template_name = 'bookshelf/book_details.html' template_name = 'bookshelf/book_details.html'
class BooksCreateView(CreateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class BooksUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
class AuthorListView(ListView): class AuthorListView(ListView):
model = Author model = Author
template_name = 'bookshelf/authors.html' template_name = 'bookshelf/authors.html'
...@@ -31,4 +45,20 @@ class AuthorDetailsView(DetailView): ...@@ -31,4 +45,20 @@ class AuthorDetailsView(DetailView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['books'] = Books.objects.all() context['books'] = Books.objects.all()
return context return context
\ No newline at end of file
class AuthorCreateView(CreateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.html'
class AuthorUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
def get_absolute_url(self):
return redirect('bookshelf:edit-author', kwargs={'pk': self.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