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
from django.urls import reverse
import datetime
class Books(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey('Authors', on_delete=models.PROTECT)
publisher = models.CharField(max_length=255)
year_published = models.IntegerField(validators = [MaxValueValidator(2023), MinValueValidator(1000)])
class Books(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey('Authors', on_delete=models.PROTECT)
publisher = models.CharField(max_length=255)
year_published = models.IntegerField(validators = [MaxValueValidator(2023), MinValueValidator(1000)])
isbn = models.CharField(max_length=13, validators = [MinLengthValidator(13)])
blurb = models.CharField(max_length=800)
def __str__(self):
return self.title
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)
last_name = models.CharField(max_length=255)
age = models.IntegerField(validators = [MaxValueValidator(200), MinValueValidator(1)])
......@@ -38,4 +31,4 @@ class Authors(models.Model):
return author_fullname
def get_absolute_url(self):
return reverse ('bookshelf:authors-details', kwargs = {'pk': self.pk})
\ No newline at end of file
return reverse ('bookshelf:authors-details', kwargs = {'pk': self.id})
\ No newline at end of file
......@@ -2,12 +2,14 @@
{% block title %}
{{ object.__str() }}
{{ object.first_name }} {{ object.last_name }}
{% endblock %}
{% block header %}
<h1>{{ object.first_name }} {{ object.last_name }}</h1>
{% endblock %}
{% block content %}
<h1>{{ object.__str() }}</h1>
<br>
<ul>
<li>Age: {{ object.age }}</li>
......@@ -17,4 +19,15 @@
<li>{{ object.bio }}</li>
</ul>
<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 %}
\ No newline at end of file
......@@ -6,12 +6,22 @@
{% endblock %}
{% block header %}
<h1>Brendan's list of favorite authors</h1>
{% endblock %}
{% block content %}
<ul>
{% for object in authors %}
<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>
{% endfor %}
</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 %}
\ No newline at end of file
......@@ -6,15 +6,25 @@
{% endblock %}
{% block content %}
{% block header %}
<h1>{{ object.title }}</h1>
<br>
{% endblock %}
{% block content %}
<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>Year Published: {{ object.year_published }}</li>
<li>ISBN: {{ object.isbn }}</li>
<br>
<li>Blurb: {{ object.blurb ]}</li>
<li>Blurb: {{ object.blurb }}</li>
</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 %}
\ No newline at end of file
......@@ -6,12 +6,22 @@
{% endblock %}
{% block header %}
<h1>Brendan's list of favorite books</h1>
{% endblock %}
{% block content %}
<ul>
{% for object in books %}
<li>
<a href "{{ object.get_absolute_url }}"> {{ object.title }}
<a href "http://127.0.0.1:8000/books/{{object.pk}}"> {{ object.title }} </a>
</li>
{% endfor %}
</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 %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}
Welcome to Brendan's favorite Books and Authors!
My Favorite Books & Authors
{% endblock %}
{% block header %}
Welcome to Brendan's favorite Books and Authors!
{% endblock %}
{% 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>
<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 %}
\ No newline at end of file
......@@ -2,6 +2,7 @@ from django.shortcuts import render
from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.shortcuts import get_object_or_404
from .models import Books, Authors
......@@ -31,10 +32,11 @@ class BookDetailView(DetailView):
class AuthorDetailView(DetailView):
model = Authors
template_name = 'bookshelf/authors_details.html'
"""
def get_context_data(self, **kwargs):
context = super(BookDetailView, self).get_context_data(**kwargs)
context[books_authored] = Books.objects.filter(author=self.object)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
books = get_object_or_404(Books, pk=self.kwargs.get('pk'))
context["object"] = context
context["object"] = object
return context
"""
# Create your views here.
No preview for this file type
......@@ -3,14 +3,12 @@
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>My Favorite {{ page_title }}!</title>
<title>{% block title %}{% endblock %}</title>
</head>
<body style = "background-color:#F0FFFF;">
<div id = "title">
{% block title %}
{% block header %}
{% endblock %}
</div>
<div id = "content">
<br>
{% block content %}
{% endblock %}
</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