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 Fritzie Dianne Del Pilar, 211983, CSCI40-F
Lab 03: My Favorite Books and Authors Lab 04: My Favorite Books and Authors v2
March 28, 2023 April 25, 2023
I, Fritzie Dianne Del Pilar, truthfully completed this lab work with the best of my ability and knowledge. 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 <sgd> Fritzie Dianne Del Pilar, April 25, 2023
\ No newline at end of file \ 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 { ...@@ -22,7 +22,7 @@ h2 {
} }
body { body {
background-image: url("/static/bookshelf/bg.jpg"); background-image: url("/static/bookshelf/books.jpg");
background-repeat: no-repeat; background-repeat: no-repeat;
background-size: cover; background-size: cover;
background-position: center; background-position: center;
...@@ -36,6 +36,13 @@ a{ ...@@ -36,6 +36,13 @@ a{
font-size:25px; font-size:25px;
} }
a:hover{
color: red;
font-family: monaco;
font-weight: bold;
font-size:25px;
}
ol { ol {
font-size:40px; font-size:40px;
font-family: monaco; 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 @@ ...@@ -7,10 +7,14 @@
<head> <head>
<h1><u>{{author.first_name}} {{author.last_name}}</u></h1> <h1><u>{{author.first_name}} {{author.last_name}}</u></h1>
<h2 >{{ author.age }} years old</h2> <h2 >{{ author.age }} years old</h2>
<h2>Nationality: {{ author.nationality }} </h2> <h2>{{ author.nationality }} </h2>
<h2>Bio: <br> {{ author.bio }} </h2> <h2>Bio: <br> {{ author.bio }} </h2>
</head> </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> <body>
<p> Books by {{author.first_name}} {{author.last_name}} I love: <p> Books by {{author.first_name}} {{author.last_name}} I love:
<ul> <ul>
...@@ -24,10 +28,9 @@ ...@@ -24,10 +28,9 @@
</ul> </ul>
</p> </p>
<div class="flex-parent jc-center"> <div class = "flex-parent jc-center">
<button onclick="location.href = '/bookshelf/home'" class="green margin-left">Home</button> <a href="{% url 'bookshelf:home' %}" type="button" class="margin-left">Home</a>
<button onclick="location.href = '/bookshelf/home/books'" class="blue">Books</button> <a href="{% url 'bookshelf:books' %}" type="button" class="jc-center">Books</a>
<button onclick="location.href = '/bookshelf/home/authors'" class="pink margin-right">Authors</button>
</div> </div>
</body> </body>
......
...@@ -13,11 +13,14 @@ ...@@ -13,11 +13,14 @@
<h2> "{{ book.blurb }}" </h2> <h2> "{{ book.blurb }}" </h2>
</head> </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> <body>
<div class="flex-parent jc-center"> <div class="flex-parent jc-center">
<button onclick="location.href = '/bookshelf/home'" class="green margin-left">Home</button> <a href="{% url 'bookshelf:home' %}" type="button" class="btn btn-outline-secondary">Home</a>
<button onclick="location.href = '/bookshelf/home/books'" class="blue">Books</button> <a href="{% url 'bookshelf:authors' %}" type="button" class="btn btn-outline-secondary">Authors</a>
<button onclick="location.href = '/bookshelf/home/authors'" class="pink margin-right">Authors</button>
</div> </div>
</body> </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 @@ ...@@ -18,8 +18,20 @@
</p> </p>
<div class="flex-parent jc-center"> <div class="flex-parent jc-center">
<button onclick="location.href = 'books/'" class="blue margin-right">Books</button> <a href="{% url 'bookshelf:books' %}" type="button" class="jc-center">Books</a>
<button onclick="location.href = 'authors/'" class="pink">Authors</button> <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> </div>
</body> </body>
......
...@@ -4,10 +4,14 @@ from .views import home ...@@ -4,10 +4,14 @@ from .views import home
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
path('home/books/', views.BooksView.as_view(), name='books'), path('home/books/', views.BooksView.as_view(), name="books"),
path('home/authors/', views.AuthorsView.as_view(), name='authors'), path('home/authors/', views.AuthorsView.as_view(), name="authors"),
path('<int:book_id>/book_details', views.BooksDetailView.as_view(), name="book_detail"), 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('<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' app_name = 'bookshelf'
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render, reverse
from django.http import HttpResponse, Http404 from django.http import Http404
from .models import Author, Book from .models import Author, Book
from django.views import View 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): def home(request):
return render(request, "bookshelf/home.html") return render(request, "bookshelf/home.html")
...@@ -36,3 +40,49 @@ class AuthorsDetailView(View): ...@@ -36,3 +40,49 @@ class AuthorsDetailView(View):
raise Http404("Author does not exist!") raise Http404("Author does not exist!")
books_list= Book.objects.all().filter(author=author) books_list= Book.objects.all().filter(author=author)
return render(request, "bookshelf/author_detail.html", {"author":author,"books_list":books_list}) 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