Commit ab820820 authored by Izaac Daniel B. Muncal's avatar Izaac Daniel B. Muncal

Merge branch 'lab04' into 'master'

Lab04

See merge request !1
parents 30710e36 8b9043b7
Izaac Daniel B. Muncal | 214138 | CSCI 40-E Izaac Daniel B. Muncal | 214138 | CSCI 40-E
Lab 3 - My Favorite Books and Authors Lab 4 - My Favorite Books and Authors v2
March 28, 2023 April 27, 2023
I independently worked on my project. I only consulted to the given materials in the modules for help. I independently worked on my project. I only consulted to the given materials in the modules for help.
<sgd> Izaac Daniel B. Muncal March 28, 2023 <sgd> Izaac Daniel B. Muncal April 27, 2023
\ No newline at end of file \ No newline at end of file
from django import forms
from .models import Author, Books
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
class BookForm(forms.ModelForm):
class Meta:
model = Books
fields = '__all__'
from django.db import models from django.db import models
from django.urls import reverse
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=255) first_name = models.CharField(max_length=255)
...@@ -10,6 +11,12 @@ class Author(models.Model): ...@@ -10,6 +11,12 @@ class Author(models.Model):
def __str__(self): def __str__(self):
return "{} {}".format(self.first_name, self.last_name) return "{} {}".format(self.first_name, self.last_name)
def get_absolute_url(self):
return reverse('bookshelf:author-details', kwargs = {'pk':self.pk})
def get_update_url(self):
return reverse('bookshelf:author-edit', kwargs = {'pk':self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
author = models.ForeignKey(Author, on_delete=models.CASCADE) author = models.ForeignKey(Author, on_delete=models.CASCADE)
...@@ -20,3 +27,10 @@ class Books(models.Model): ...@@ -20,3 +27,10 @@ class Books(models.Model):
def __str__(self): def __str__(self):
return "{}".format(self.title) return "{}".format(self.title)
def get_absolute_url(self):
return reverse('bookshelf:book-details', kwargs={'pk': self.pk})
def get_update_url(self):
return reverse('bookshelf:book-edit', kwargs={'pk': self.pk})
{% extends 'base.html' %}
{% load static %}
{% block title %} Add New Author {% endblock %}
{% block header %}{% endblock %}
{% block body %}
{{ 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 %}
{% for field in form %}
{{field.label}}: {{field}}<br><br>
{% endfor %}
&nbsp; &nbsp; &nbsp; &nbsp;<input type="submit" value="Add Author">
</form>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Add New Book {% endblock %}
{% block header %}{% endblock %}
{% block body %}
{{ 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 %}
{% for field in form %}
{{field.label}}: {{field}}<br><br>
{% endfor %}
&nbsp; &nbsp; &nbsp; &nbsp;<input type="submit" value="Add Book">
</form>
{% endblock %}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<p>{{object.age}}</p> <p>{{object.age}}</p>
<p>{{object.nationality}}</p> <p>{{object.nationality}}</p>
<p>{{object.bio}}</p> <p>{{object.bio}}</p>
<a href="/bookshelf/author/{{author.pk}}/edit"><button>Edit Author</button></a><br><br>
<p><b>Books by {{object.first_name}} {{object.last_name}} I love:</b></p> <p><b>Books by {{object.first_name}} {{object.last_name}} I love:</b></p>
<ul> <ul>
{% for book in object.books_set.all %} {% for book in object.books_set.all %}
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
<p>{{object.year_published}}</p> <p>{{object.year_published}}</p>
<p>{{object.ISBN}}</p> <p>{{object.ISBN}}</p>
<p>{{object.blurb}}</p> <p>{{object.blurb}}</p>
<a href="/bookshelf/books/{{books.pk}}/edit"><button>Edit Book</button></a><br><br>
<a href="/bookshelf/">Home</a> <a href="/bookshelf/">Home</a>
<a href="/bookshelf/books/">Books</a> <a href="/bookshelf/books/">Books</a>
......
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Author {% endblock %}
{% block header %}{% endblock %}
{% block body %}
{{ 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 %}
{% for field in form %}
{{field.label}}: {{field}}<br><br>
{% endfor %}
&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Save Changes">
</form>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Book {% endblock %}
{% block header %}{% endblock %}
{% block body %}
{{ 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 %}
{% for field in form %}
{{field.label}}: {{field}}<br><br>
{% endfor %}
&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Save Changes">
</form>
{% endblock %}
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
{% block body %} {% block body %}
<p> I don't read books so these are the only books I have read so far.</p> <p> I don't read books so these are the only books I have read so far.</p>
<a href="/bookshelf/books/">Books</a>----------------------------------------- <a href="/bookshelf/books/">Books</a>-----------------------------------------
<a href="/bookshelf/author/">Authors</a> <a href="/bookshelf/authors/">Authors</a>
<p>
<a href="/bookshelf/books/add">Add Book</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href = "/bookshelf/authors/add">Add Author</a>
</p>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import home_view, BooksView, BooksDetailView, AuthorView, AuthorDetailView from .views import home_view, BooksView, BooksDetailView, AuthorView, AuthorDetailView, BookCreateView, BookUpdateView, \
AuthorCreateView, AuthorUpdateView
urlpatterns = [ urlpatterns = [
path('', home_view, name='home'), path('', home_view, name='home'),
path('books/', BooksView.as_view(), name='books'), path('books/', BooksView.as_view(), name='books'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='book-details'), path('books/<int:pk>/details/', BooksDetailView.as_view(), name='book-details'),
path('author/', AuthorView.as_view(), name='author'), path('authors/', AuthorView.as_view(), name='author'),
path('author/<int:pk>/details/', AuthorDetailView.as_view(), name='author-details'), path('author/<int:pk>/details/', AuthorDetailView.as_view(), name='author-details'),
path('books/add/', BookCreateView.as_view(), name='add-book'),
path('authors/add/', AuthorCreateView.as_view(), name='add-author'),
path('books/<pk>/edit/', BookUpdateView.as_view(), name='edit-book'),
path('author/<pk>/edit/', AuthorUpdateView.as_view(), name='edit-author'),
] ]
# This might be needed depending on your Django version # This might be needed depending on your Django version
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
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.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse
from .models import Books, Author from .models import Books, Author
...@@ -9,17 +11,48 @@ def home_view(request): ...@@ -9,17 +11,48 @@ def home_view(request):
return render(request, 'bookshelf/home.html', {'nickname': 'Dani'}) return render(request, 'bookshelf/home.html', {'nickname': 'Dani'})
class AuthorView(ListView):
model = Author
template_name = 'bookshelf/author.html'
class AuthorDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_detail.html'
class BooksView(ListView): class BooksView(ListView):
model = Books model = Books
template_name = 'bookshelf/books.html'
class BooksDetailView(DetailView): class BooksDetailView(DetailView):
model = Books model = Books
template_name = 'bookshelf/books_detail.html'
class AuthorView(ListView): class AuthorCreateView(CreateView):
model = Author model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.html'
class AuthorDetailView(DetailView): class BookCreateView(CreateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class AuthorUpdateView(UpdateView):
model = Author model = Author
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
class BookUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
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