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