Commit 8cbbe2ce authored by Fritzie Dianne Del Pilar's avatar Fritzie Dianne Del Pilar

Merge branch 'lab04'

parents d61be466 e863fc92
Fritzie Dianne Del Pilar, 211983, CSCI40-F
Lab 03: My Favorite Books and Authors
March 28, 2023
Lab 04: My Favorite Books and Authors v2
April 25, 2023
I, Fritzie Dianne Del Pilar, truthfully completed this lab work with the best of my ability and knowledge.
<sgd> Fritzie Dianne Del Pilar, March 27, 2023
\ No newline at end of file
<sgd> Fritzie Dianne Del Pilar, April 25, 2023
\ No newline at end of file
from django.forms import ModelForm
from .models import Author, Book
class BookForm(ModelForm):
class Meta:
model = Book
fields = ["title", "author", "publisher", "year_published", "ISBN", "blurb", ]
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = ["first_name", "last_name", "age", "nationality", "bio", ]
......@@ -22,7 +22,7 @@ h2 {
}
body {
background-image: url("/static/bookshelf/bg.jpg");
background-image: url("/static/bookshelf/books.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
......@@ -36,6 +36,13 @@ a{
font-size:25px;
}
a:hover{
color: red;
font-family: monaco;
font-weight: bold;
font-size:25px;
}
ol {
font-size:40px;
font-family: monaco;
......
{% extends "base.html" %}
{% block page-title %}Add New Author{% endblock %}
{% block content %}
<form method="POST" action="{% url 'bookshelf:add_author' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button class="button" type="Add Author">Add Author</button>
</form>
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% block page-title %}Add New Book{% endblock %}
{% block content %}
<form method="POST" action="{% url 'bookshelf:add_book' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button class="button" type="Add Book">Add Book</button>
</form>
{% endblock %}
\ No newline at end of file
......@@ -7,10 +7,14 @@
<head>
<h1><u>{{author.first_name}} {{author.last_name}}</u></h1>
<h2 >{{ author.age }} years old</h2>
<h2>Nationality: {{ author.nationality }} </h2>
<h2>{{ author.nationality }} </h2>
<h2>Bio: <br> {{ author.bio }} </h2>
</head>
<div class="flex-parent jc-center">
<a href="{% url 'bookshelf:edit_author' author.id %}" type="button" class="btn btn-outline-secondary">Edit Author</a>
</div>
<body>
<p> Books by {{author.first_name}} {{author.last_name}} I love:
<ul>
......@@ -24,10 +28,9 @@
</ul>
</p>
<div class="flex-parent jc-center">
<button onclick="location.href = '/bookshelf/home'" class="green margin-left">Home</button>
<button onclick="location.href = '/bookshelf/home/books'" class="blue">Books</button>
<button onclick="location.href = '/bookshelf/home/authors'" class="pink margin-right">Authors</button>
<div class = "flex-parent jc-center">
<a href="{% url 'bookshelf:home' %}" type="button" class="margin-left">Home</a>
<a href="{% url 'bookshelf:books' %}" type="button" class="jc-center">Books</a>
</div>
</body>
......
......@@ -13,11 +13,14 @@
<h2> "{{ book.blurb }}" </h2>
</head>
<div class="flex-parent jc-center">
<a href="{% url 'bookshelf:edit_book' book.id %}" type="button" class="btn btn-outline-secondary">Edit Book</a>
</div>
<body>
<div class="flex-parent jc-center">
<button onclick="location.href = '/bookshelf/home'" class="green margin-left">Home</button>
<button onclick="location.href = '/bookshelf/home/books'" class="blue">Books</button>
<button onclick="location.href = '/bookshelf/home/authors'" class="pink margin-right">Authors</button>
<a href="{% url 'bookshelf:home' %}" type="button" class="btn btn-outline-secondary">Home</a>
<a href="{% url 'bookshelf:authors' %}" type="button" class="btn btn-outline-secondary">Authors</a>
</div>
</body>
......
{% extends "base.html" %}
{% block page-title %}Edit Author{% endblock %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button class="button" type="submit">Save Changes</button>
</form>
{% endblock %}
{% extends "base.html" %}
{% block page-title %}Edit Book{% endblock %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button class="button" type="submit">Save Changes</button>
</form>
{% endblock %}
......@@ -18,8 +18,20 @@
</p>
<div class="flex-parent jc-center">
<button onclick="location.href = 'books/'" class="blue margin-right">Books</button>
<button onclick="location.href = 'authors/'" class="pink">Authors</button>
<a href="{% url 'bookshelf:books' %}" type="button" class="jc-center">Books</a>
<a href="{% url 'bookshelf:authors' %}" type="button" class="margin-right">Authors</a>
</div>
<div class="flex-parent jc-center">
<form method="post" action="books/add">
{% csrf_token %}
<button type="submit">Add Book</button>
</form>
<form method="post" action="authors/add">
{% csrf_token %}
<button type="submit">Add Author</button>
</form>
</div>
</body>
......
......@@ -4,10 +4,14 @@ from .views import home
urlpatterns = [
path('home/', home, name='home'),
path('home/books/', views.BooksView.as_view(), name='books'),
path('home/authors/', views.AuthorsView.as_view(), name='authors'),
path('home/books/', views.BooksView.as_view(), name="books"),
path('home/authors/', views.AuthorsView.as_view(), name="authors"),
path('<int:book_id>/book_details', views.BooksDetailView.as_view(), name="book_detail"),
path('<int:author_id>/author_details', views.AuthorsDetailView.as_view(), name="author_detail"),
path('home/books/add', views.AddBookView.as_view(), name="add_book"),
path('home/authors/add', views.AddAuthorView.as_view(), name="add_author"),
path('books/<int:pk>/edit/', views.BookEditView.as_view(), name="edit_book"),
path('authors/<int:pk>/edit/', views.AuthorEditView.as_view(), name="edit_author"),
]
app_name = 'bookshelf'
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse, Http404
from django.shortcuts import render, reverse
from django.http import Http404
from .models import Author, Book
from django.views import View
from .forms import BookForm, AuthorForm
from django.shortcuts import get_object_or_404
from django.views.generic.edit import UpdateView
def home(request):
return render(request, "bookshelf/home.html")
......@@ -35,4 +39,50 @@ class AuthorsDetailView(View):
except Author.DoesNotExist:
raise Http404("Author does not exist!")
books_list= Book.objects.all().filter(author=author)
return render(request, "bookshelf/author_detail.html", {"author":author,"books_list":books_list})
\ No newline at end of file
return render(request, "bookshelf/author_detail.html", {"author":author,"books_list":books_list})
class AddBookView(View):
model = Book
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['form'] = BookForm()
return context
def post(self, request,*args,**kwargs):
form = BookForm(request.POST)
if form.is_valid():
new_book = form.save()
return render(request, "bookshelf/book_detail.html", {'book': new_book})
else:
return render(request, "bookshelf/add-book.html", {'form': form})
class AddAuthorView(View):
model = Author
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context['form'] = AuthorForm()
return context
def post(self, request,*args,**kwargs):
form = AuthorForm(request.POST)
if form.is_valid():
new_author = form.save()
return render(request, "bookshelf/author_detail.html", {'author': new_author})
else:
return render(request, "bookshelf/add-author.html", {'form': form})
class BookEditView(UpdateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
def get_success_url(self):
return reverse('bookshelf:book_detail', kwargs={'book_id':self.object.pk})
class AuthorEditView(UpdateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
def get_success_url(self):
return reverse('bookshelf:author_detail', kwargs={'author_id':self.object.pk})
\ 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