Commit c1006fc4 authored by Albert Gagalac's avatar Albert Gagalac

Added 'books by author' in books-details

parent 4cf4b889
......@@ -19,7 +19,7 @@ class Author(models.Model):
class Book(models.Model):
title = models.CharField(default="", max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
publisher = models.CharField(default="", max_length=100)
ISBN = models.PositiveIntegerField(validators=
[RegexValidator(r'^[0-9]*$',
......@@ -32,6 +32,3 @@ class Book(models.Model):
def get_absolute_url(self):
return reverse('bookshelf:books-detail', kwargs={'pk' : self.pk})
def get_home_url(self):
return reverse('bookshelf:books-list')
......@@ -4,6 +4,7 @@
{% block css %}
h1 {
padding-top: 2%;
font-family:'Comic Sans MS',cursive;
}
h3 {
font-family:'Comic Sans MS',cursive;
......@@ -14,9 +15,17 @@
{% block title %}{{object}}{% endblock %}
{% block heading %}{{object}}{% endblock %}
{% block content %}
<h3>Age:</h3> {{object.age}}<br>
<h3>Nationality:</h3> {{object.nationality}}<br>
<h3>Bio:</h3> {{object.bio}}
<h3>Age:</h3> {{object.age}}<br>
<h3>Nationality:</h3> {{object.nationality}}<br>
<h3>Bio:</h3> {{object.bio}}
<h1>Books by {{object}} I love:</h1>
{% for book in book_list %}
<a href="{{ book.get_absolute_url }}">
{{book.title}}
</a>
<br><br>
{% endfor %}
<br><br><br><a href="/home/ ">Home</a> | <a href="/books/ ">Books</a> | <a href="/authors/ ">Authors</a>
{% endblock %}
\ No newline at end of file
......@@ -4,7 +4,6 @@
{% block css %}
h1 {
padding-top: 80px;
color: deep-blue;
}
{% endblock %}
......@@ -12,8 +11,10 @@
{% block heading %}Burt's Favorite Books{% endblock %}
{% block content %}
{% for object in object_list %}
<a href="{{ object.get_absolute_url }}">{{object.title}}</a>
<br><br>
<a href="{{ object.get_absolute_url }}">
{{object.title}}
</a>
<br><br>
{% endfor %} <br>
<a href="/home/ ">Home</a> | <a href="/authors/">Authors</a>
......
......@@ -5,7 +5,6 @@ from .views import (
home, BookListView, AuthorListView
)
urlpatterns = [
path('', home, name='home'),
path('home/', home, name='home'),
path("authors/", AuthorListView.as_view(), name="author-list"),
path("books/", BookListView.as_view(), name="books-list"),
......
......@@ -16,6 +16,11 @@ class BookDetailView(DetailView):
class AuthorDetailView(DetailView):
model = Author
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['book_list'] = Book.objects.filter(author=self.object)
return context
class BookListView(ListView):
model = Book
......
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