Commit c57d3ce5 authored by Angelo Alvarez's avatar Angelo Alvarez

Added Views and Templates for the Model Author

parent be6c8b2a
...@@ -12,6 +12,9 @@ class Author(models.Model): ...@@ -12,6 +12,9 @@ class Author(models.Model):
def __str__(self): def __str__(self):
return self.first_name + " " + self.last_name return self.first_name + " " + self.last_name
def get_absolute_url(self):
return reverse('bookshelf:authors-details', kwargs={'pk': self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=50) title = models.CharField(max_length=50)
author = models.ForeignKey(Author, on_delete=models.CASCADE) author = models.ForeignKey(Author, on_delete=models.CASCADE)
......
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ object.first_name }} {{ object.last_name }}{% endblock %}
{% block content %}
<h1>{{ object.first_name }} {{ object.last_name }}</h1>
<b>Age: </b> {{ object.age }}<br>
<b>Nationality: </b>{{ object.nationality }} <br>
{{ object.bio }}
<hr>
Books by {{ object.first_name }} {{ object.last_name }} I love:
<ul>
{% for book in books %}
{% if book.author.first_name == object.first_name and book.author.last_name == object.last_name %}
<li><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
<div id="links" style="margin: auto; text-align: center; width: 100%">
<a href="/bookshelf/home/">Home</a>
&nbsp; &nbsp;
<a href="/bookshelf/books/">Books</a>
&nbsp; &nbsp;
<a href="/bookshelf/authors/">Authors</a>
</div>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h1>Alva's Favorite Authors</h1>
<ul>
{% for object in object_list %}
<li><a href="{{ object.get_absolute_url }}">{{ object.first_name }} {{ object.last_name }}</a></li>
{% endfor %}
</ul>
<hr>
<div id="links" style="margin: auto; text-align: center; width: 100%">
<a href="/bookshelf/home/">Home</a>
&nbsp; &nbsp;
<a href="/bookshelf/authors/">Authors</a>
</div>
{% endblock %}
...@@ -4,19 +4,19 @@ ...@@ -4,19 +4,19 @@
{% block title %}{{ object.title }}{% endblock %} {% block title %}{{ object.title }}{% endblock %}
{% block content %} {% block content %}
<h1>{{ object.title }}</h1> <h1>{{ object.title }}</h1>
<em>Author: </em><a href="{{ object.author.get_absolute_url }}">{{ object.author }}</a> <br> <b>Author: </b><a href="{{ object.author.get_absolute_url }}">{{ object.author }}</a> <br>
<em>Publisher: </em>{{ object.publisher }} <br> <b>Publisher: </b>{{ object.publisher }} <br>
<em>year_published: </em>{{ object.year_published }} <br> <b>Year published: </b>{{ object.year_published }} <br>
<em>ISBN: </em>{{ object.ISBN }} <br> <b>ISBN: </b>{{ object.ISBN }} <br>
{{ object.blurb }} {{ object.blurb }}
<hr> <hr>
<div id="links" style="margin: auto; text-align: center; width: 100%"> <div id="links" style="margin: auto; text-align: center; width: 100%">
<a href="../home/">Home</a> <a href="/bookshelf/home/">Home</a>
&nbsp; &nbsp; &nbsp; &nbsp;
<a href="../books/">Books</a> <a href="/bookshelf/books/">Books</a>
&nbsp; &nbsp; &nbsp; &nbsp;
<a href="../authors/">Authors</a> <a href="/bookshelf/authors/">Authors</a>
</div> </div>
{% endblock %} {% endblock %}
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
<hr> <hr>
<div id="links" style="margin: auto; text-align: center; width: 100%"> <div id="links" style="margin: auto; text-align: center; width: 100%">
<a href="../home/">Home</a> <a href="/bookshelf/home/">Home</a>
&nbsp; &nbsp; &nbsp; &nbsp;
<a href="../authors/">Authors</a> <a href="/bookshelf/authors/">Authors</a>
</div> </div>
{% endblock %} {% endblock %}
from django.urls import path from django.urls import path
from .views import home, BooksListView, BooksDetailView from .views import home, BooksListView, BooksDetailView, AuthorListView, AuthorDetailView
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
path('books/', BooksListView.as_view(), name='books-list'), path('books/', BooksListView.as_view(), name='books-list'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-details') path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-details'),
path('authors/', AuthorListView.as_view(), name='authors-list'),
path('authors/<int:pk>/details/', AuthorDetailView.as_view(), name='authors-details')
] ]
# This might be needed, depending on your Django version # This might be needed, depending on your Django version
......
...@@ -3,15 +3,22 @@ from django.views import View ...@@ -3,15 +3,22 @@ from django.views import View
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from .models import Books from .models import Books, Author
# Create your views here. # Create your views here.
def home(request): def home(request):
return render(request, 'home.html') return render(request, 'bookshelf/home.html')
class BooksListView(ListView): class BooksListView(ListView):
model = Books model = Books
class BooksDetailView(DetailView): class BooksDetailView(DetailView):
model = Books model = Books
\ No newline at end of file
class AuthorListView(ListView):
model = Author
class AuthorDetailView(DetailView):
model = Author
booklist = Books.objects.all()
extra_context = { 'books' : booklist }
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