Created webpage templates for bookshelf app

parent 7df5f732
# Generated by Django 4.1.7 on 2023-03-28 08:48
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0002_alter_book_year_published'),
]
operations = [
migrations.AlterField(
model_name='book',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='books', to='bookshelf.author'),
),
]
from django.db import models from django.db import models
from django.core.validators import MaxValueValidator, MinLengthValidator from django.core.validators import MaxValueValidator, MinLengthValidator
from django.urls import reverse
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=100) first_name = models.CharField(max_length=100)
...@@ -11,9 +12,12 @@ class Author(models.Model): ...@@ -11,9 +12,12 @@ class Author(models.Model):
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) title = models.CharField(max_length=100)
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) publisher = models.CharField(max_length=100)
year_published = models.PositiveIntegerField(validators=[MaxValueValidator(2023)]) year_published = models.PositiveIntegerField(validators=[MaxValueValidator(2023)])
ISBN = models.IntegerField(validators=[MaxValueValidator(9999999999999)]) ISBN = models.IntegerField(validators=[MaxValueValidator(9999999999999)])
...@@ -26,3 +30,6 @@ class Book(models.Model): ...@@ -26,3 +30,6 @@ class Book(models.Model):
def __str__(self): def __str__(self):
return '{} by {}'.format(self.title, self.author) return '{} by {}'.format(self.title, self.author)
def get_absolute_url(self):
return reverse('bookshelf:book-details', kwargs={'pk': self.pk})
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{object.first_name}} {{object.last_name}}{% endblock %}
{% block content %}
<h1>{{ object.first_name }} {{ object.last_name }}</h1>
<h2>Age: {{object.age}} </h2>
<h2>Nationality: {{object.nationality}} </h2>
<h2>Bio: {{object.bio}} </h2>
<h2>Books by {{object.first_name}} {{object.last_name}} I love:</h2>
<ul>
{% for book in object.books.all %}
<li>
<a href="{{ book.get_absolute_url }}"> {{book.title}}</a>
</li>
{% endfor %}
</ul>
<a href="/bookshelf/home/">Home</a>
<a href="/bookshelf/books/">Books</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h2>Neil's Favorite Authors</h2>
<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="/bookshelf/home/">Home</a>
<a href="/bookshelf/books/">Books</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}This is the book details page{% endblock %}
{% block content %}
<h1>{{object.title}}</h1>
<h2>Author: <a href="{{ object.author.get_absolute_url }}"> {{object.author.first_name}} {{object.author.last_name}}</a> </h2>
<h2>Publisher: {{object.publisher}} </h2>
<h2>Year Published: {{object.year_published}} </h2>
<h2>ISBN: {{object.ISBN}} </h2>
<h2>Blurb: {{object.blurb}} </h2>
<a href="/bookshelf/home/">Home</a>
<a href="/bookshelf/books/">Books</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}This is the authors page{% endblock %}
{% block content %}
<h1>Books Object List</h1>
<br><br>
<ul>
{% for object in object_list %}
<li>
<a href="{{ object.get_absolute_url }}">{{ object.title }}</a>
</li>
{% endfor %}
</ul>
<a href="/bookshelf/home/">Home</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books & Authors{% endblock %}
{% block content %}
<h1>Welcome to Neil's Database of Favorite Books and Authors</h1>
<p> My go-to authors and books are generally those that ara already famous or well-known. <br>
While it might seem that I'm a trendchaser, I'd like to think that I just don't want to <br>
be disappointed. While I do enjoy the calmness that comes with reading, I still prioritize <br>
making every second of it worthwhile.</p>
<a href="/bookshelf/books/">Books</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import home, AuthorsView, AuthorDetailsView, BooksView, BookDetailsView
urlpatterns = [ urlpatterns = [
path('', index, name='bookshelf'), path('home/', home, name='home'),
path('authors/', AuthorsView.as_view(), name='author-list'),
path('author/<int:pk>/details/', AuthorDetailsView.as_view(), name='author-details'),
path('books/', BooksView.as_view(), name='book-list'),
path('book/<int:pk>/details/', BookDetailsView.as_view(), name='book-details'),
] ]
app_name = "<bookshelf>" app_name = "bookshelf"
\ No newline at end of file \ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
def index(request): from .models import Author, Book
return HttpResponse('This is the bookshelf app')
def home(request):
return render(request, 'bookshelf/home.html', {'name': 'Home Page'})
class AuthorsView(ListView):
model = Author
template_name = 'bookshelf/authors.html'
class BooksView(ListView):
model = Book
template_name = 'bookshelf/books.html'
class AuthorDetailsView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
class BookDetailsView(DetailView):
model = Book
template_name = 'bookshelf/book_details.html'
\ No newline at end of file
...@@ -59,7 +59,7 @@ ROOT_URLCONF = 'neilocampo_reading.urls' ...@@ -59,7 +59,7 @@ ROOT_URLCONF = 'neilocampo_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': [
...@@ -120,7 +120,8 @@ USE_TZ = True ...@@ -120,7 +120,8 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/ # https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
......
<html lang="en">
<head>
<title>{% block title %}My Favorite Books & Authors{% endblock %}</title>
{% block styles %}{% endblock %}
</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