Commit bddd89a3 authored by Brendan Fausto's avatar Brendan Fausto

Added list of books authored by each author and cleaned code

parent 13cf5257
# Generated by Django 4.1.6 on 2023-04-13 12:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='authors',
name='id',
),
migrations.RemoveField(
model_name='books',
name='id',
),
migrations.AddField(
model_name='authors',
name='auto_increment_id',
field=models.AutoField(default=0, primary_key=True, serialize=False),
preserve_default=False,
),
migrations.AddField(
model_name='books',
name='auto_increment_id',
field=models.AutoField(default=0, primary_key=True, serialize=False),
preserve_default=False,
),
]
# Generated by Django 4.1.6 on 2023-04-13 13:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0002_remove_authors_id_remove_books_id_and_more'),
]
operations = [
migrations.RemoveField(
model_name='authors',
name='auto_increment_id',
),
migrations.RemoveField(
model_name='books',
name='auto_increment_id',
),
migrations.AddField(
model_name='authors',
name='id',
field=models.BigAutoField(auto_created=True, default=0, primary_key=True, serialize=False, verbose_name='ID'),
preserve_default=False,
),
migrations.AddField(
model_name='books',
name='id',
field=models.BigAutoField(auto_created=True, default=0, primary_key=True, serialize=False, verbose_name='ID'),
preserve_default=False,
),
]
...@@ -3,29 +3,22 @@ from django.core.validators import MaxValueValidator, MinValueValidator, MinLeng ...@@ -3,29 +3,22 @@ from django.core.validators import MaxValueValidator, MinValueValidator, MinLeng
from django.urls import reverse from django.urls import reverse
import datetime import datetime
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=255)
title = models.CharField(max_length=255) author = models.ForeignKey('Authors', on_delete=models.PROTECT)
publisher = models.CharField(max_length=255)
author = models.ForeignKey('Authors', on_delete=models.PROTECT) year_published = models.IntegerField(validators = [MaxValueValidator(2023), MinValueValidator(1000)])
publisher = models.CharField(max_length=255)
year_published = models.IntegerField(validators = [MaxValueValidator(2023), MinValueValidator(1000)])
isbn = models.CharField(max_length=13, validators = [MinLengthValidator(13)]) isbn = models.CharField(max_length=13, validators = [MinLengthValidator(13)])
blurb = models.CharField(max_length=800) blurb = models.CharField(max_length=800)
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self): def get_absolute_url(self):
return reverse('bookshelf:books-details', kwargs = {'pk': self.pk}) return reverse('bookshelf:books-details', kwargs = {'pk': self.id})
class Authors(models.Model): class Authors(models.Model):
first_name = models.CharField(max_length=255) first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255)
age = models.IntegerField(validators = [MaxValueValidator(200), MinValueValidator(1)]) age = models.IntegerField(validators = [MaxValueValidator(200), MinValueValidator(1)])
...@@ -38,4 +31,4 @@ class Authors(models.Model): ...@@ -38,4 +31,4 @@ class Authors(models.Model):
return author_fullname return author_fullname
def get_absolute_url(self): def get_absolute_url(self):
return reverse ('bookshelf:authors-details', kwargs = {'pk': self.pk}) return reverse ('bookshelf:authors-details', kwargs = {'pk': self.id})
\ No newline at end of file \ No newline at end of file
...@@ -2,12 +2,14 @@ ...@@ -2,12 +2,14 @@
{% block title %} {% block title %}
{{ object.__str() }} {{ object.first_name }} {{ object.last_name }}
{% endblock %} {% endblock %}
{% block header %}
<h1>{{ object.first_name }} {{ object.last_name }}</h1>
{% endblock %}
{% block content %} {% block content %}
<h1>{{ object.__str() }}</h1>
<br> <br>
<ul> <ul>
<li>Age: {{ object.age }}</li> <li>Age: {{ object.age }}</li>
...@@ -17,4 +19,15 @@ ...@@ -17,4 +19,15 @@
<li>{{ object.bio }}</li> <li>{{ object.bio }}</li>
</ul> </ul>
<br> <br>
<h2>Books authored by {{ object.first_name }} {{ object.last_name }}:
{% for book in books %}
{% if book.author == self.pk %}
<li>
<a href "http://127.0.0.1:8000/books/{{object.pk}}"> {{ object.title }} </a>
</li>
{% endif %}
{% endfor %}
<br>
<a href = "http://127.0.0.1:8000/books">Books</a>
<a href = "http://127.0.0.1:8000/authors">Authors</a>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -6,12 +6,22 @@ ...@@ -6,12 +6,22 @@
{% endblock %} {% endblock %}
{% block header %}
<h1>Brendan's list of favorite authors</h1>
{% endblock %}
{% block content %} {% block content %}
<ul> <ul>
{% for object in authors %} {% for object in authors %}
<li> <li>
<a href "{{ object.get_absolute_url }}">{{ object.first_name }} {{ object.last_name }} <a href "{{ object.get_absolute_url }}">{{ object.first_name }} {{ object.last_name }} </a>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<p>
<a href = "http://127.0.0.1:8000/">Home</a>
<a href = "http://127.0.0.1:8000/books">Books</a>
</p>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -6,15 +6,25 @@ ...@@ -6,15 +6,25 @@
{% endblock %} {% endblock %}
{% block content %} {% block header %}
<h1>{{ object.title }}</h1> <h1>{{ object.title }}</h1>
<br> {% endblock %}
{% block content %}
<ul> <ul>
<li>Author: <a href="{{ object.author.get_absolute_url }}""> {{ object.author }}</a></li> <li>Author: <a href="{{ object.author.get_absolute_url }}"> {{ object.author }}</a></li>
<li>Publisher: {{ object.publisher }}</li> <li>Publisher: {{ object.publisher }}</li>
<li>Year Published: {{ object.year_published }}</li> <li>Year Published: {{ object.year_published }}</li>
<li>ISBN: {{ object.isbn }}</li> <li>ISBN: {{ object.isbn }}</li>
<br> <br>
<li>Blurb: {{ object.blurb ]}</li>
<li>Blurb: {{ object.blurb }}</li>
</ul> </ul>
<p>
<a href = "http://127.0.0.1:8000/books">Books</a>
<a href = "http://127.0.0.1:8000/authors">Authors</a>
</p>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -6,12 +6,22 @@ ...@@ -6,12 +6,22 @@
{% endblock %} {% endblock %}
{% block header %}
<h1>Brendan's list of favorite books</h1>
{% endblock %}
{% block content %} {% block content %}
<ul> <ul>
{% for object in books %} {% for object in books %}
<li> <li>
<a href "{{ object.get_absolute_url }}"> {{ object.title }} <a href "http://127.0.0.1:8000/books/{{object.pk}}"> {{ object.title }} </a>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<p>
<a href = "http://127.0.0.1:8000/">Home</a>
<a href = "http://127.0.0.1:8000/authors">Authors</a>
</p>
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends 'base.html' %} {% extends 'base.html' %}
{% block title %} {% block title %}
Welcome to Brendan's favorite Books and Authors! My Favorite Books & Authors
{% endblock %} {% endblock %}
{% block header %}
Welcome to Brendan's favorite Books and Authors!
{% endblock %}
{% block content %} {% block content %}
<p>Test blurb about books and their genres, as well as the authors behind the books here</p> <p>I don't really have a preference to genres of books, so long as they are either insightful or entertaining to read.</p>
<br> <br>
<p>
<a href = "http://127.0.0.1:8000/books">Books</a> <a href = "http://127.0.0.1:8000/books">Books</a>
<a href = "http://127.0.0.1:8000/authors">Authors</a> <a href = "http://127.0.0.1:8000/authors">Authors</a>
</p>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -2,6 +2,7 @@ from django.shortcuts import render ...@@ -2,6 +2,7 @@ from django.shortcuts import render
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.shortcuts import get_object_or_404
from .models import Books, Authors from .models import Books, Authors
...@@ -31,10 +32,11 @@ class BookDetailView(DetailView): ...@@ -31,10 +32,11 @@ class BookDetailView(DetailView):
class AuthorDetailView(DetailView): class AuthorDetailView(DetailView):
model = Authors model = Authors
template_name = 'bookshelf/authors_details.html' template_name = 'bookshelf/authors_details.html'
"""
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(BookDetailView, self).get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context[books_authored] = Books.objects.filter(author=self.object) books = get_object_or_404(Books, pk=self.kwargs.get('pk'))
context["object"] = context
context["object"] = object
return context return context
"""
# Create your views here. # Create your views here.
No preview for this file type
...@@ -3,14 +3,12 @@ ...@@ -3,14 +3,12 @@
<html lang="en"> <html lang="en">
<head> <head>
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
<title>My Favorite {{ page_title }}!</title> <title>{% block title %}{% endblock %}</title>
</head> </head>
<body style = "background-color:#F0FFFF;"> <body style = "background-color:#F0FFFF;">
<div id = "title"> {% block header %}
{% block title %}
{% endblock %} {% endblock %}
</div> <br>
<div id = "content">
{% block content %} {% block content %}
{% endblock %} {% endblock %}
</div> </div>
......
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