Commit e95161e9 authored by Paolo Luis R. Encisa's avatar Paolo Luis R. Encisa

Finished the Lab

parent 4b7cef5a
from django import forms
from django.urls import reverse_lazy
from .models import Books, Author
from django.views.generic.edit import UpdateView
class BookForm(forms.ModelForm):
class Meta:
model = Books
fields = '__all__'
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
class BookUpdate(UpdateView):
model = Books
form_class = BookForm
template_name = 'bookshelf/edit-book.html'
def form_valid(self, form):
form.save()
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy('book_details', kwargs={'pk': self.object.pk})
# Generated by Django 4.2 on 2023-04-24 13:31
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0002_alter_books_author'),
]
operations = [
migrations.AlterField(
model_name='books',
name='blurb',
field=models.CharField(max_length=1000),
),
]
......@@ -16,7 +16,7 @@ class Books(models.Model):
publisher = models.CharField(max_length=100)
year_published = models.IntegerField()
ISBN = models.CharField(max_length=13)
blurb = models.CharField(max_length=200)
blurb = models.CharField(max_length=1000)
def __str__(self):
return self.title
{% extends 'bookshelf/base.html' %}
{% block title %}Add New Author{% endblock %}
{% block content %}
<h1>Add New Author</h1>
<form method="post">
{% csrf_token %}
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" class="form-control" id="first_name" name="first_name">
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" class="form-control" id="last_name" name="last_name">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="number" class="form-control" id="age" name="age">
</div>
<div class="form-group">
<label for="nationality">Nationality:</label>
<input type="text" class="form-control" id="nationality" name="nationality">
</div>
<div class="form-group">
<label for="bio">Bio:</label>
<textarea class="form-control" id="bio" name="bio"></textarea>
</div>
<button type="submit" class="btn btn-primary">Add Author</button>
</form>
{% endblock %}
{% extends 'bookshelf/base.html' %}
{% block title %}Add New Book{% endblock %}
{% block content %}
<h1>Add New Book</h1>
<form method="post">
{% csrf_token %}
<div class="form-group">
<label for="{{ form.title.id_for_label }}">Title:</label>
{{ form.title }}
</div>
<div class="form-group">
<label for="{{ form.author.id_for_label }}">Author:</label>
{{ form.author }}
</div>
<div class="form-group">
<label for="{{ form.publisher.id_for_label }}">Publisher:</label>
{{ form.publisher }}
</div>
<div class="form-group">
<label for="{{ form.year_published.id_for_label }}">Year Published:</label>
{{ form.year_published }}
</div>
<div class="form-group">
<label for="{{ form.ISBN.id_for_label }}">ISBN:</label>
{{ form.ISBN }}
</div>
<div class="form-group">
<label for="{{ form.blurb.id_for_label }}">Blurb:</label>
{{ form.blurb }}
</div>
<button type="submit" class="btn btn-primary">Add Book</button>
</form>
{% endblock %}
......@@ -8,10 +8,14 @@
<p>Nationality: {{ author.nationality }}</p>
<p>{{ author.bio }}</p>
<div>
<a href="{% url 'edit_author' author.pk %}">Edit Author</a>
</div>
<h2>Books by {{ author.first_name }} {{ author.last_name }} I Love:</h2>
<ul>
{% for book in author.books.all %}
<li><a href="{% url 'book_details' book.pk %}">{{ book.title }}</a></li>
<li><a href="{% url 'books_details' book.pk %}">{{ book.title }}</a></li>
{% empty %}
<li>No books found for {{ author.first_name }} {{ author.last_name }}</li>
{% endfor %}
......@@ -19,6 +23,5 @@
<ul>
<li><a href="{% url 'home' %}">Home</a>&emsp; <a href="{% url 'books' %}">Books</a> &emsp; <a href="{% url 'authors' %}">Authors</a></li>
</ul>
{% endblock %}
......@@ -4,7 +4,13 @@
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
<div class="main-content">
{% block content %}
{% endblock %}
</div>
<div class="sidebar">
{% block sidebar %}
{% endblock %}
</div>
</body>
</html>
......@@ -6,7 +6,7 @@
<h2>Pao's Favorite Books:</h2>
<ul>
{% for book in books %}
<li><a href="{% url 'book_details' book.pk %}">{{ book.title }}</a></li>
<li><a href="{% url 'books_details' book.pk %}">{{ book.title }}</a></li>
{% endfor %}
</ul>
......
......@@ -12,8 +12,7 @@
{% block sidebar %}
<h3>Navigation</h3>
<ul>
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'books' %}">Books</a></li>
<li><a href="{% url 'authors' %}">Authors</a></li>
<li><a href="{% url 'edit_book' pk=book.pk %}" class="btn btn-primary">Edit Book</a></li>
<li><a href="{% url 'home' %}">Home</a>&emsp; <a href="{% url 'books' %}">Books</a> &emsp; <a href="{% url 'authors' %}">Authors</a></li>
</ul>
{% endblock %}
{% extends 'bookshelf/base.html' %}
{% block title %}Edit Author{% endblock %}
{% block content %}
<h1>Edit Author</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Save Changes</button>
</form>
{% endblock %}
{% extends 'bookshelf/base.html' %}
{% block content %}
<h1>Edit Book</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
......@@ -7,6 +7,6 @@
<p>Greetings and a warm welcome to my Books Database! As an avid reader, I have a great passion for the fantasy genre, particularly works by literary titans such as J.K. Rowling and Rick Riordan.</p>
<ul>
<li><a href="{% url 'books' %}">Books</a> &emsp; <a href="{% url 'authors' %}">Authors</a></li>
<li><a href="{% url 'books_add' %}">Add Book</a> &emsp; <a href="{% url 'authors_add' %}">Add Author</a></li>
</ul>
{% endblock %}
......@@ -4,7 +4,11 @@ from . import views
urlpatterns = [
path('', views.home, name='home'),
path('books/', views.BookListView.as_view(), name='books'),
path('books/<int:pk>/details/', views.BookDetailView.as_view(), name='book_details'),
path('books/<int:pk>/details/', views.BookDetailView.as_view(), name='books_details'),
path('books/<int:pk>/update/', views.BookUpdateView.as_view(), name='edit_book'),
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('authors/<int:pk>/', views.AuthorDetailView.as_view(), name='author_details'),
path('authors/<int:pk>/update/', views.AuthorUpdateView.as_view(), name='edit_author'),
path('books/add/', views.add_book, name='books_add'),
path('authors/add/', views.add_author, name='authors_add'),
]
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django import forms
from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView, UpdateView
from django.urls import reverse_lazy
from .models import Books, Author
from .forms import BookForm, AuthorForm
class BookListView(ListView):
model = Books
template_name = 'bookshelf/books.html'
context_object_name = 'books'
class BookDetailView(DetailView):
model = Books
template_name = 'bookshelf/books_details.html'
......@@ -17,11 +22,34 @@ class BookDetailView(DetailView):
context['book'] = self.get_object()
return context
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 BookForm(forms.ModelForm):
author = forms.ModelChoiceField(queryset=Author.objects.all())
class Meta:
model = Books
fields = ['title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['author'].widget.attrs.update({'class': 'form-control'})
self.fields['author'].widget = forms.Select(attrs={'class': 'form-control'}, choices=[(author.pk, author) for author in Author.objects.all()])
class AuthorListView(ListView):
model = Author
template_name = 'bookshelf/authors.html'
context_object_name = 'authors'
class AuthorDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
......@@ -32,6 +60,23 @@ class AuthorDetailView(DetailView):
context['author'] = self.get_object()
return context
class AuthorUpdateView(UpdateView):
model = Author
form_class = AuthorForm
template_name = 'bookshelf/edit-author.html'
def get_success_url(self):
return reverse_lazy('author_details', kwargs={'pk': self.object.pk})
class EditAuthorView(UpdateView):
model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio']
template_name = 'bookshelf/edit-author.html'
success_url = reverse_lazy('author_details')
def home(request):
context = {
'title': 'My Favorite Books & Authors',
......@@ -41,3 +86,27 @@ def home(request):
'authors_link': 'authors/'
}
return render(request, 'bookshelf/home.html', context=context)
def add_book(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
books = form.save()
return redirect('books_details', pk=books.pk)
else:
form = BookForm()
context = {'form': form}
return render(request, 'bookshelf/add-book.html', context=context)
def add_author(request):
if request.method == 'POST':
form = AuthorForm(request.POST)
if form.is_valid():
author = form.save()
return redirect('author_details', pk=author.pk)
else:
form = AuthorForm()
context = {'form': form}
return render(request, 'bookshelf/add-author.html', context=context)
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