Commit ea8a6d44 authored by Gabriel Geraldo's avatar Gabriel Geraldo

added templates and views for bookshelf pages

parent 704c2091
from django.db import models from django.db import models
from django.core.validators import MinValueValidator,MaxValueValidator, MinLengthValidator, MaxLengthValidator from django.core.validators import MinValueValidator,MaxValueValidator, MinLengthValidator, MaxLengthValidator
from django.utils.timezone import datetime from django.utils.timezone import datetime
from django.urls import reverse
# Create your models here. # Create your models here.
class Author(models.Model): class Author(models.Model):
...@@ -13,9 +14,12 @@ class Author(models.Model): ...@@ -13,9 +14,12 @@ class Author(models.Model):
def __str__(self): def __str__(self):
return f"{self.first_name} {self.last_name}" return f"{self.first_name} {self.last_name}"
def get_absolute_url(self):
return reverse("bookshelf:author_details", kwargs={'pk':self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
author = models.ForeignKey(Author, on_delete=models.CASCADE) author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')
publisher = models.CharField(max_length=255) publisher = models.CharField(max_length=255)
year_published = models.PositiveIntegerField( year_published = models.PositiveIntegerField(
validators=[MaxValueValidator(datetime.today().year)] validators=[MaxValueValidator(datetime.today().year)]
...@@ -30,3 +34,7 @@ class Books(models.Model): ...@@ -30,3 +34,7 @@ class Books(models.Model):
def __str__(self): def __str__(self):
return f"{self.title} by {self.author}" return f"{self.title} by {self.author}"
def get_absolute_url(self):
return reverse("bookshelf:book_details", kwargs={'pk':self.pk})
{% extends 'base.html' %}
{% block title %}{{ object.first_name }} {{ object.last_name }}{% endblock %}
{% block content %}
<h1>{{ object.first_name }} {{ object.last_name }}</h1>
<p>{{ object.age }}</p>
<p>{{ object.nationality }}</p>
<p>{{ object.bio}}</p>
<h1>Books by {{ object.first_name }} {{ object.last_name }} I love</h1>
<ul>
{% for book in object.books.all %}
<li>
<a href="{{ book.get_absolute_url }}">{{ book.title }}</a>
</li>
{% endfor %}
</ul>
<a href="../../home">Home</a>
<a href="../../books">Books</a>
<a href="../">Authors</a>
{% endblock %}
{% extends 'base.html' %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h1>{{ nickname }}'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>
<a href="../home">Home</a>
<a href="../books">Books</a>
{% endblock %}
{% extends 'base.html' %}
{% block title %}{{ object.title }}{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<p>{{ object.author }}</p>
<p>{{ object.publisher }}</p>
<p>{{ object.year_published}}</p>
<p>{{ object.isbn }}</p>
<p>{{ object.blurb }}</p>
<a href="../../home">Home</a>
<a href="../">Books</a>
<a href="../../authors">Authors</a>
{% endblock %}
{% extends 'base.html' %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h1>{{ nickname }}'s Favorite Books:</h1>
<ul>
{% for object in object_list %}
<li><a href="{{ object.get_absolute_url }}">{{ object.title }}</a></li>
{% endfor %}
</ul>
<a href="../home">Home</a>
<a href="../authors">Authors</a>
{% endblock %}
{% extends 'base.html' %}
{% block title %}My Favorite Books & Authors{% endblock %}
{% block content %}
<h1>Welcome to {{ nickname }}'s Database of Favorite Books and Authors!</h1>
<p>I really like etc and etc</p>
<a href="../books">Books</a>
<a href="../authors">Authors</a>
{% endblock %}
{% extends 'base.html' %}
{% block scripts %}
<meta http-equiv="Refresh" content="0; url='home/" />
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import index_view, home_view, BooksListView, BooksDetailView, AuthorListView, AuthorDetailView
urlpatterns = [ urlpatterns = [
path('', index, name='index') path('', index_view, name='index'),
path('home/', home_view, name='home'),
path('books/', BooksListView.as_view(template_name='bookshelf/books.html'), name='books'),
path('books/<int:pk>/details', BooksDetailView.as_view(template_name='bookshelf/book_details.html'), name='book_details'),
path('authors/', AuthorListView.as_view(template_name='bookshelf/authors.html'), name='authors'),
path('authors/<int:pk>/details', AuthorDetailView.as_view(template_name='bookshelf/author_details.html'), name='author_details'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from .models import Books, Author
# Create your views here. # Create your views here.
def index(request): def index_view(request):
return HttpResponse('initialized bookshelf views') return render(request, "bookshelf/index.html", {})
\ No newline at end of file
def home_view(request):
return render(request, "bookshelf/home.html", {'nickname' : 'Gab'})
class BooksListView(ListView):
model = Books
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['nickname'] = 'Gab'
return ctx
class BooksDetailView(DetailView):
model = Books
class AuthorListView(ListView):
model = Author
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['nickname'] = 'Gab'
return ctx
class AuthorDetailView(DetailView):
model = Author
\ No newline at end of file
...@@ -59,7 +59,7 @@ ROOT_URLCONF = 'gabgeraldo_reading.urls' ...@@ -59,7 +59,7 @@ ROOT_URLCONF = 'gabgeraldo_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>
<head>
<title>{% block title %}My amazing site{% endblock %}</title>
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
{% block scripts %}{% 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