Commit 61954df0 authored by Washington99's avatar Washington99

Author and Books details

Created the template for the views of Author and Books models
parent 5adc7b82
......@@ -12,9 +12,12 @@ class Author(models.Model):
def __str__(self):
return "{} {}".format(self.first_name, self.last_name)
def get_absolute_url(self):
return str(self.pk)
class Books(models.Model):
title = models.CharField(max_length = 50)
author = models.ForeignKey(Author, on_delete = models.CASCADE)
author = models.ForeignKey(Author, on_delete = models.CASCADE, related_name = "books")
publisher = models.CharField(max_length = 50)
year_published = models.IntegerField()
ISBN = models.CharField(max_length = 14)
......@@ -22,3 +25,6 @@ class Books(models.Model):
def __str__(self):
return "{} ({}, {})".format(self.title, self.author, self.year_published)
def get_absolute_url(self):
return str(self.pk)
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} {{object}} {% endblock %}
{% block content %}
<h2>{{object}}</h2>
<div>
<p>Age: {{object.age}}</p>
<p>Nationality: {{object.nationality}}</p>
<p>Bio: {{object.bio}}</p>
</div>
<div>
<p>Books by {{object}} that I love:</p>
{% for book in object.books.all %}
<p><a href="../../../../books/{{book.get_absolute_url}}/details/">
{{book.title}}
</a></p>
{% endfor %}
</div>
<a href="/home">Home</a>
<a href="/books">Books</a>
<a href="/authors">Authors</a>
{% endblock %}
\ No newline at end of file
......@@ -8,7 +8,7 @@
<div>
{% for object in object_list %}
<p><a href = "">
<p><a href = "{{object.get_absolute_url}}/details/">
{{object}}
</a></p>
{% endfor %}
......
{% extends 'base.html' %}
{% load static %}
{% block title %} {{object.title}} {% endblock %}
{% block content %}
<h2>{{object.title}}</h2>
<p><a href="../../../../authors/{{object.author.get_absolute_url}}/details/">
by {{object.author}}
</a> </p>
<div>
<p>Published by {{object.publisher}} in {{object.year_published}}</p>
<p>ISBN: {{object.ISBN}}</p>
</div>
<div>
<p>{{object.blurb}}</p>
</div>
<a href="/home">Home</a>
<a href="/books">Books</a>
<a href="/authors">Authors</a>
{% endblock %}
\ No newline at end of file
......@@ -9,8 +9,8 @@
<div>
{% for object in object_list %}
<p><a href = "">
{{object}}
<p><a href = "{{object.get_absolute_url}}/details/">
{{object.title}}
</a></p>
{% endfor %}
</div>
......
from django.urls import path
from .views import home_view, AuthorList, BookList
from .views import home_view, AuthorList, BookList, AuthorView, BookView
urlpatterns = [
path('home/', home_view, name = 'home_view'),
path('authors/', AuthorList.as_view(), name = 'authors'),
path('books/', BookList.as_view(), name = 'books'),
path('authors/<int:pk>/details/', AuthorView.as_view(), name = 'authors_details'),
path('books/<int:pk>/details/', BookView.as_view(), name = 'books_details'),
]
app_name = "bookshelf"
......
......@@ -17,3 +17,11 @@ class BookList(ListView):
model = Books
template_name = 'bookshelf/books.html'
class AuthorView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
class BookView(DetailView):
model = Books
template_name = 'bookshelf/book_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