Commit fb2b132c authored by Tanya Yotoko's avatar Tanya Yotoko

Added template views and urls of app bookshelf, added CSS styling via bootstrap

parent 9c88de86
from django.db import models from django.db import models
from django.core.validators import MinValueValidator,MaxValueValidator,MinLengthValidator from django.core.validators import MinValueValidator,MaxValueValidator,MinLengthValidator
from django.urls import reverse
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=100,default="") first_name = models.CharField(max_length=100,default="")
...@@ -9,15 +10,21 @@ class Author(models.Model): ...@@ -9,15 +10,21 @@ class Author(models.Model):
bio = models.TextField(max_length=700,default="") bio = models.TextField(max_length=700,default="")
def __str__(self): def __str__(self):
return '{}'.format(self.first_name,self.last_name) return '{} {}'.format(self.first_name,self.last_name)
def get_absolute_url(self):
return reverse("bookshelf:author_details",kwargs={'pk':self.pk})
class Book(models.Model): class Book(models.Model):
title = models.CharField(max_length=100,default="") title = models.CharField(max_length=100,default="")
author = models.ForeignKey(Author,on_delete=models.CASCADE) author = models.ForeignKey(Author,on_delete=models.CASCADE,related_name='books')
publisher = models.CharField(max_length=100,default="") publisher = models.CharField(max_length=100,default="")
year_published = models.PositiveIntegerField(validators=[MaxValueValidator(2023)]) year_published = models.PositiveIntegerField(validators=[MaxValueValidator(2023)])
ISBN = models.PositiveIntegerField(unique=True,validators=[MaxValueValidator(9999999999999),MinValueValidator(1000000000000)]) ISBN = models.PositiveIntegerField(unique=True,validators=[MaxValueValidator(9999999999999),MinValueValidator(1000000000000)])
blurb = models.TextField(validators=[MinLengthValidator(100)],max_length=200,default="") blurb = models.TextField(validators=[MinLengthValidator(100)],max_length=200,default="")
def __str__(self): def __str__(self):
return '{}'.format(self.title,self.author) return '{}'.format(self.title,self.author)
\ No newline at end of file
def get_absolute_url(self):
return reverse("bookshelf:book_details",kwargs={'pk':self.pk})
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}{{object.first_name}} {{object.last_name}}{% endblock %}
{% block content %}
<ul>
<li> <h2> {{object.first_name}} {{object.last_name}} </h2> </li>
<li> <h4> {{object.age}} </h4> </li>
<li> <h4> {{object.nationality}} </h4> </li>
<li> <h4> {{object.bio}} </h4> </li>
<li> <h4> Books by {{object.first_name}} {{object.last_name}} I love: </h4> </li>
<ul>
{% for book in object.books.all %}
<li>
<a href="{{ book.get_absolute_url }}"> {{book.title}} </a>
</li>
{% endfor %}
</ul>
<h2>
<a href ="/bookshelf/home/" class="btn btn-primary" role="button"> Home </a>
<a href ="/bookshelf/books/" class="btn btn-primary" role="button"> Books </a>
<a href ="/bookshelf/authors/" class="btn btn-primary" role="button"> Author </a>
</h2>
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h2> Tanya's Favorite Authors: </h2>
<ul class="list-group">
{% for object in object_list %}
<li class="list-group-item">
<a href="{{ object.get_absolute_url }}"> {{object.first_name}} {{object.last_name}} </a>
</li>
{% endfor %}
<h2>
<a href ="/bookshelf/home/" class="btn btn-primary" role="button"> Home </a>
<a href ="/bookshelf/books/" class="btn btn-primary" role="button"> Books </a>
</h2>
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}{{object.title}}{% endblock %}
{% block content %}
<ul>
<li> <h3> {{object.title}} </h3> </li>
<li> <a href ="{{ object.author.get_absolute_url }}"> <h4> {{object.author}} </h4> </a> </li>
<li> <h4> {{object.publisher}} </h4> </li>
<li> <h4> {{object.year_published}} </h4> </li>
<li> <h4> {{object.ISBN}} </h4> </li>
<li> <h4> {{object.blurb}} </h4> </li>
<a href ="/bookshelf/home/" class="btn btn-primary" role="button"> Home </a>
<a href ="/bookshelf/books/" class="btn btn-primary" role="button"> Books </a>
<a href ="/bookshelf/authors/" class="btn btn-primary" role="button"> Authors </a>
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h2> Tanya's Favorite Books </h2>
<ul class="list-group">
{% for object in object_list %}
<li class="list-group-item">
<a href="{{ object.get_absolute_url }}"> {{object.title}} </a>
</li>
{% endfor %}
<h3>
<a href="/bookshelf/home/" class="btn btn-primary" role="button"> Home </a>
<a href="/bookshelf/authors" class="btn btn-primary" role="button"> Authors </a>
</h3>
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}My Favorite Books & Authors{% endblock %}
{% block content %}
<h1 class="text-primary">Welcome to Tanya's Database of Favorite Books and Authors</h1>
<br>
<h4> I am a mood reader and I like to explore genres when I have the time! </h4>
<br>
<a href="/bookshelf/books/" class="btn btn-primary" role="button">Books</a>
<a href="/bookshelf/authors/" class="btn btn-primary" role="button">Authors</a>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index, homepage, BooksView, AuthorsView, BooksDetailView, AuthorsDetailView
from .views import index
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', index, name='index'),
path('home/',homepage,name='home'),
path('books/',BooksView.as_view(),name='books'),
path('authors/',AuthorsView.as_view(),name='authors'),
path('books/<int:pk>/details/',BooksDetailView.as_view(),name='book_details'),
path('authors/<int:pk>/details/',AuthorsDetailView.as_view(),name='author_details'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render
from .models import Author, Book
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
def index(request): def index(request):
return HttpResponse('Hello! This came from the index view') return HttpResponse('Hello! This came from the index view')
\ No newline at end of file
def homepage(request):
return render(request, 'bookshelf/home.html', {'name':'homepage'})
class BooksView(ListView):
model = Book
template_name = "bookshelf/books.html"
class BooksDetailView(DetailView):
model = Book
template_name = "bookshelf/book_details.html"
class AuthorsView(ListView):
model = Author
template_name = "bookshelf/authors.html"
class AuthorsDetailView(DetailView):
model = Author
template_name = "bookshelf/author_details.html"
...@@ -58,7 +58,7 @@ ROOT_URLCONF = 'tanyayotoko_reading.urls' ...@@ -58,7 +58,7 @@ ROOT_URLCONF = 'tanyayotoko_reading.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width="device-width",initial-scale=1.0">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
\ No newline at end of file
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