Commit 03e3540e authored by justin's avatar justin

Created detail HTML pages for each author, fixed URL routing for authors in book pages

parent 5ee8933e
...@@ -18,6 +18,12 @@ class Author(models.Model): ...@@ -18,6 +18,12 @@ class Author(models.Model):
def __str__(self): def __str__(self):
return f"{self.first_name} {self.last_name}" return f"{self.first_name} {self.last_name}"
def get_absolute_url(self):
return reverse(
"author-detail",
kwargs={"pk": self.pk},
)
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
......
{% extends 'base.html' %}
{% block title %} {{ object.first_name }} {{ object.last_name }} {% endblock %}
{% block content %}
<div class="author-details">
<h1>{{ object }}</h1>
<ul>
<li>{{ object.age }}</li>
<li>{{ object.nationality }}</li>
<li>{{ object.bio }}</li>
</ul>
</div>
<div class="author-books">
<h1>Books by {{ object }} I love</h1>
<ul>
{% for book in object.books_set.all %}
<a href="{{book.get_absolute_url}}"><li>{{book.title}}</li></a>
{% endfor %}
</ul>
</div>
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/books">Books</a></li>
<li><a href="/authors">Authors</a></li>
</ul>
{% endblock %}
{% extends 'base.html' %}
{% block title %} My Favorite Authors {% endblock %}
{% block content %}
<h1>Justin' Favorite Authors</h1>
<ul>
{% for object in object_list %}
<a href="{{object.get_absolute_url}}"><li>{{object.first_name}} {{object.last_name}}</li></a>
{% endfor %}
</ul>
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/books">Books</a></li>
</ul>
{% endblock %}
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<div class="book-details"> <div class="book-details">
<h1>{{ object.title }}</h1> <h1>{{ object.title }}</h1>
<ul> <ul>
<li>{{ object.author }}</li> <a href="{{object.author.get_absolute_url}}"><li>{{ object.author }}</li></a>
<li>{{ object.publisher }}</li> <li>{{ object.publisher }}</li>
<li>{{ object.year_published }}</li> <li>{{ object.year_published }}</li>
<li>{{ object.isbn }}</li> <li>{{ object.isbn }}</li>
......
...@@ -3,14 +3,14 @@ from .views import ( ...@@ -3,14 +3,14 @@ from .views import (
index, index,
BookListView, BookListView,
BookDetailView, BookDetailView,
AuthorListView,
AuthorDetailView,
) )
urlpatterns = [ urlpatterns = [
path("home/", index, name="home"), path("home/", index, name="home"),
path( path("books/", BookListView.as_view(), name="book-list"),
"books/",
BookListView.as_view(),
name="book-list",
),
path("books/<int:pk>/detail", BookDetailView.as_view(), name="book-detail"), path("books/<int:pk>/detail", BookDetailView.as_view(), name="book-detail"),
path("authors/", AuthorListView.as_view(), name="author-list"),
path("author/<int:pk>/detail", AuthorDetailView.as_view(), name="author-detail"),
] ]
...@@ -12,16 +12,6 @@ def index(request): ...@@ -12,16 +12,6 @@ def index(request):
) )
def books(request):
bookList = Books.objects.all()
context = {"bookList": bookList}
return render(
request,
"bookshelf/books.html",
context,
)
class BookListView(ListView): class BookListView(ListView):
model = Books model = Books
template_name = "bookshelf/books.html" template_name = "bookshelf/books.html"
...@@ -32,13 +22,11 @@ class BookDetailView(DetailView): ...@@ -32,13 +22,11 @@ class BookDetailView(DetailView):
template_name = "bookshelf/book_details.html" template_name = "bookshelf/book_details.html"
# class BooksView(View): class AuthorListView(ListView):
# def get(self, request): model = Author
# books = Books.objects.all() template_name = "bookshelf/authors.html"
# context = {"books": books}
# return render(request, "bookshelf/books.html", context)
# class HomePageView(View): class AuthorDetailView(DetailView):
# def get(self, request): model = Author
# return (render, "bookshelf/home.html") template_name = "bookshelf/author_details.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