Commit 494ccdd8 authored by Eury See's avatar Eury See

Created and layout the templates, View, and URLs of the bookshelf pages.

parent bc0d1add
Pipeline #3101 canceled with stages
from django.db import models
from django.urls import reverse
class Author(models.Model):
first_name = models.CharField(max_length = 50)
......@@ -8,11 +9,14 @@ class Author(models.Model):
bio = models.TextField(max_length = 700)
def __str__(self):
return self.last_name
return '{} {}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return reverse('bookshelf:author-detail', kwargs={'pk':self.pk})
class Books(models.Model):
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)
year_published = models.IntegerField()
ISBN = models.IntegerField()
......@@ -20,3 +24,6 @@ class Books(models.Model):
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('bookshelf:books-detail', kwargs={'pk':self.pk})
\ No newline at end of file
{% extends 'bookshelf/base.html' %}
{% load static %}
{% block title %} {{ object.first_name }} {{ object.last_name }} {% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'styles.css' %"}>
<div class="center">
<h1>{{ object.first_name }} {{ object.last_name }}</h1>
</div>
<li>
{{ object.age }}
</li>
<li>
{{ object.nationality }}
</li>
<li>
{{ object.bio }}
</li>
<p>Books by {{ object.first_name }} {{ object.last_name }} that I love:</p>
{% for book in author.books.all %}
<li><a href="{% url 'bookshelf:books-detail' pk=book.pk %}">{{ book.title }}</a></li>
{% endfor %}
<p style="text-align: center;"><a href="http://127.0.0.1:8000/bookshelf/home">Home</a> <a href="http://127.0.0.1:8000/bookshelf/books">Books</a> <a href="http://127.0.0.1:8000/bookshelf/authors">Authors</a></p>
{% block styles %}
<style>
body {
background-color: rgb(202, 255, 206);
font-family: Georgia, serif;
font-size: 16px;
color: rgb(90, 154, 78);
}
</style>
{% endblock %}
{% endblock %}
\ No newline at end of file
{% extends 'bookshelf/base.html' %}
{% load static %}
{% block title %} My Favorite Authors {% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'styles.css' %"}>
<h1>Eury's Favorite Authors:</h1>
{% for object in authors %}
<div>
<li><a href="{{ object.get_absolute_url }}">{{ object.first_name }} {{ object.last_name }}</a></li>
{% endfor %}
<p style="text-align: center;"><a href="http://127.0.0.1:8000/bookshelf/home">Home</a> <a href="http://127.0.0.1:8000/bookshelf/books">Books</a></p>
</div>
{% block styles %}
<style>
body {
background-color: rgb(159, 209, 255);
font-family: Georgia, serif;
font-size: 16px;
color: rgb(58, 97, 205);
}
</style>
{% endblock %}
{% endblock %}
<html lang="en">
<head>
<link rel="stylesheet" href="{% static 'styles.css' %"}>
<title>{% block title %}{% endblock %}</title>
{% block styles %}{% endblock %}
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>
\ No newline at end of file
{% extends 'bookshelf/base.html' %}
{% load static %}
{% block title %} My Favorite Books {% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'styles.css' %"}>
<h1>Eury's Favorite Books:</h1>
{% for object in books %}
<li>
<a href="{{ object.get_absolute_url }}">
{{ object.title }}
</a>
</li>
{% endfor %}
<p style="text-align: center;"><a href="http://127.0.0.1:8000/bookshelf/home">Home</a> <a href="http://127.0.0.1:8000/bookshelf/authors">Authors</a></p>
{% block styles %}
<style>
body {
background-color: rgb(226, 207, 255);
font-family: Georgia, serif;
font-size: 16px;
color: rgb(162, 98, 178);
}
</style>
{% endblock %}
{% endblock %}
\ No newline at end of file
{% extends 'bookshelf/base.html' %}
{% load static %}
{% block title %} {{ object.title }} {% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'styles.css' %"}>
<h1>{{ object.title }}</h1>
<li><a href="{% url 'bookshelf:author-detail' pk=author.pk %}">{{ object.author.first_name }} {{ object.author.last_name }}</a></li>
<li>{{ object.publisher }}</li>
<li> {{ object.year_published }}</li>
<li>{{ object.ISBN }}</li>
<li>{{ object.blurb }}</li>
<p style="text-align: center;"><a href="http://127.0.0.1:8000/bookshelf/home">Home</a> <a href="http://127.0.0.1:8000/bookshelf/books">Books</a> <a href="http://127.0.0.1:8000/bookshelf/authors">Authors</a></p>
{% block styles %}
<style>
body {
background-color: rgb(255, 204, 232);
font-family: Georgia, serif;
font-size: 16px;
color: rgb(178, 92, 147);
}
</style>
{% endblock %}
{% endblock %}
\ No newline at end of file
{% extends 'bookshelf/base.html' %}
{% load static %}
{% block title %} My Favorite Books and Authors {% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'styles.css' %"}>
<div style="text-align: center;">
<h1>Welcome to Eury's Database of Favorite Books and Authors!</h1>
<p>I enjoy reading books that range from genres including crime, thriller, magical realism, and historical fiction. Some of my favorite authors are Haruki Murakami and Dan Brown.</p>
<p><a href="http://127.0.0.1:8000/bookshelf/books">Books</a> <a href="http://127.0.0.1:8000/bookshelf/authors">Authors</a></p>
</div>
{% block styles %}
<style>
body {
background-color: rgb(253, 222, 207);
font-family: Georgia, serif;
font-size: 16px;
color: rgb(206, 132, 100);
}
</style>
{% endblock %}
</div>
{% endblock %}
div {
background-color: #f79a9a;
font-family: Georgia, sans-serif;
font-size: 16px;
color: #f7ecec;
text-align: center;
}
h1 {text-align:center}
\ No newline at end of file
from django.urls import path
from .views import index
from .views import (BooksListView,BooksDetailView, AuthorListView, AuthorDetailView)
from . import views
urlpatterns = [
path('', index, name='index'),
path("home", views.home, name = "home"), #FBV
path("books", BooksListView.as_view(), name = "books"),
path("books/<int:pk>/details", BooksDetailView.as_view(), name = "books-detail"),
path("authors", AuthorListView.as_view(), name = "authors"),
path("authors/<int:pk>/details", AuthorDetailView.as_view(), name = "author-detail"),
]
# This might be needed, depending on your Django version
app_name = "bookshelf"
\ No newline at end of file
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 Books, Author
def home(request):
return render(request, 'bookshelf/home.html', {})
class BooksListView(ListView):
def get(self, request):
books = Books.objects.all()
return render(request, "bookshelf/books.html", {'books' : books})
class BooksDetailView(DetailView):
model = Books
template_name = 'bookshelf/books_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['author'] = self.object.author
return context
class AuthorListView(ListView):
def get(self, request):
authors = Author.objects.all()
return render(request, "bookshelf/authors.html", {'authors' : authors})
class AuthorDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['books'] = self.object.books
return context
\ No newline at end of file
def index(request):
return HttpResponse("")
......@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
......@@ -116,7 +117,8 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# 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
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
......
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