Commit 7a199934 authored by karin-kurusu's avatar karin-kurusu

Created "Books Page" and "Book's Details Page"

parent 64188981
from django.db import models from django.db import models
from django.urls import reverse
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=250, default="") first_name = models.CharField(max_length=250, default="")
...@@ -20,5 +21,7 @@ class Books(models.Model): ...@@ -20,5 +21,7 @@ class Books(models.Model):
def __str__(self): def __str__(self):
return "\'{}\' by {} {}".format(self.title, self.author.first_name, self.author.last_name) return "\'{}\' by {} {}".format(self.title, self.author.first_name, self.author.last_name)
def get_absolute_url(self):
return reverse('bookshelf:book_detais', args=[self.pk])
# Create your models here. # Create your models here.
{% extends 'base.html' %}
{% load static %}
{% block title %}{{book.title}}{% endblock %}
{% block content %}
<h2>
{{book.title}}
</h2>
<p>
{{author.first_name}} {{author.last_name}}
</p>
<p>{{book.publisher}}</p>
<p>{{book.year_published}}</p>
<p>{{book.ISBN}}</p>
<p>{{book.blurb}}</p>
{% 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 Books{% endblock %}
{% block content %}
<h2>
{{name}}'s Favorite Books:
</h2>
<ul>
{% for book in books_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="/authors">Authors</a></li>
</ul>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import home from .views import home, BooksList, BooksDetails
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
path('books/', BooksList.as_view(), name='books'),
path('books/<int:pk>/details', BooksDetails.as_view(), name='book_detais'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.views import View
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Author, Books from .models import Author, Books
def home(request): def home(request):
return render(request, 'home.html', {'name': 'Calvin'}) return render(request, 'home.html', {'name': 'Calvin'})
class BooksList(ListView):
def get(self, request):
books_list = Books.objects.all()
return render(request, 'bookshelf/books.html', {'name': 'Calvin', 'books_list': books_list})
class BooksDetails(DetailView):
def get(self, request, pk):
book = Books.objects.get(pk=pk)
author = book.author
return render(request, 'bookshelf/book_detais.html', {'author': author, 'book': book})
# Create your views here. # 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