Commit b8eefd3c authored by karin-kurusu's avatar karin-kurusu

Created "Authors Page" and "Author's Details Page"

parent 7a199934
......@@ -10,6 +10,8 @@ class Author(models.Model):
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return reverse('bookshelf:author_details', args=[self.pk])
class Books(models.Model):
title = models.CharField(max_length=250, default="")
......
{% extends 'base.html' %}
{% load static %}
{% block title %}{{author.first_name}} {{author.last_name}}{% endblock %}
{% block content %}
<h2>
{{author.first_name}} {{author.last_name}}
</h2>
<p>
{{author.first_name}} {{author.last_name}}
</p>
<p>{{author.age}}</p>
<p>{{author.nationality}}</p>
<p>{{author.bio}}</p>
<h2>
Books by {{author.first_name}} {{author.last_name}} I love:
</h2>
<ul>
{% for book in book_list %}
<li><a href={{book.get_absolute_url}}>{{book.title}}</a></li>
{% endfor %}
</ul>
{% endblock %}
{% block navbar %}
<ul class="navbarList">
<li class="navbarItem"><a href="/home">Home</a></li>
<li class="navbarItem"><a href="/books">Books</a></li>
<li class="navBarItem"><a href="/authors">Authors</a></li>
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h2>
{{name}}'s Favorite Authors:
</h2>
<ul>
{% for author in author_list %}
<li><a href="{{author.get_absolute_url}}">{{author.first_name}} {{author.last_name}}</a></li>
{% endfor %}
</ul>
{% endblock %}
{% block navbar %}
<ul class="navbarList">
<li class="navbarItem"><a href="/home">Home</a></li>
<li class="navBarItem"><a href="/books">Books</a></li>
</ul>
{% endblock %}
\ No newline at end of file
from django.urls import path
from .views import home, BooksList, BooksDetails
from .views import home, BooksList, BooksDetails, AuthorList, AuthorDetails
urlpatterns = [
path('home/', home, name='home'),
path('books/', BooksList.as_view(), name='books'),
path('books/<int:pk>/details', BooksDetails.as_view(), name='book_detais'),
path('authors/', AuthorList.as_view(), name="authors"),
path('authors/<int:pk>/details', AuthorDetails.as_view(), name='author_details'),
]
app_name = "bookshelf"
\ No newline at end of file
......@@ -17,4 +17,15 @@ class BooksDetails(DetailView):
book = Books.objects.get(pk=pk)
author = book.author
return render(request, 'bookshelf/book_detais.html', {'author': author, 'book': book})
class AuthorList(ListView):
def get(self, request):
author_list = Author.objects.all()
return render(request, 'bookshelf/authors.html', {'name': 'Calvin', 'author_list': author_list})
class AuthorDetails(DetailView):
def get(self, request, pk):
author = Author.objects.get(pk=pk)
book_list = Books.objects.filter(author__first_name__exact=author.first_name)
return render(request, 'bookshelf/author_details.html', {'author': author, 'book_list': book_list})
# Create your views here.
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