Created the templates

parent 67e8e19f
...@@ -2,7 +2,7 @@ Lex Philip Gabriel D. Chan ...@@ -2,7 +2,7 @@ Lex Philip Gabriel D. Chan
211411 211411
CSCI 40 - E CSCI 40 - E
Lab 03: My Favorite Books and Authors Lab 03: My Favorite Books and Authors
March 28, 2023 March 29, 2023
This laboratory project was done and truthfully completed by me. This laboratory project was done and truthfully completed by me.
<sgd> Lex Philip Gabriel D. Chan, March 28, 2023 <sgd> Lex Philip Gabriel D. Chan, March 29, 2023
\ No newline at end of file \ No newline at end of file
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=255, default="") first_name = models.CharField(max_length=255, default="")
...@@ -10,9 +11,12 @@ class Author(models.Model): ...@@ -10,9 +11,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 Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=255, default="") title = models.CharField(max_length=255, 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=255, default="") publisher = models.CharField(max_length=255, default="")
year_published = models.IntegerField(default=0) year_published = models.IntegerField(default=0)
ISBN = models.IntegerField(default=0) ISBN = models.IntegerField(default=0)
...@@ -20,3 +24,6 @@ class Books(models.Model): ...@@ -20,3 +24,6 @@ class Books(models.Model):
def __str__(self): def __str__(self):
return '{}'.format(self.title) return '{}'.format(self.title)
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 %}
<h1>{{ object.first_name }} {{ object.last_name }}</h1>
<h3>{{ object.age }}</h3>
<h3>{{ object.nationality }}</h3>
<p>{{ object.bio }}</p>
<br>
<h3>Books by {{ object.first_name }} {{ object.last_name }} I love:</h3>
<ul>
{% for book in object.books.all %}
<li>
<a href="{{ book.get_absolute_url}}">
{{ book.title }}
</a>
</li>
{% endfor %}
</ul>
<center>
<a href="/bookshelf/home">Home</a>
<a href="/bookshelf/books">Books</a>
<a href="/bookshelf/authors">Authors</a>
</center>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h1>Lex'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>
<center>
<a href="/bookshelf/home">Home</a>
<a href="/bookshelf/books">Books</a>
</center>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}{{ object.title }}{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<h3><a href="{{ object.author.get_absolute_url}}">
{{ object.author }}
</a></h3>
<h3>{{ object.publisher }}</h3>
<h3>{{ object.year_published }}</h3>
<h3>{{ object.ISBN }}</h3>
<p>{{ object.blurb }}</p>
<center>
<a href="/bookshelf/home">Home</a>
<a href="/bookshelf/books">Books</a>
<a href="/bookshelf/authors">Authors</a>
</center>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h1>Lex's Favorite Books:</h1>
<ul>
{% for object in object_list %}
<li>
<a href="{{ object.get_absolute_url}}">
{{ object.title }}
</a>
</li>
{% endfor %}
</ul>
<center>
<a href="/bookshelf/home">Home</a>
<a href="/bookshelf/authors">Authors</a>
</center>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %}My Favorite Books and Authors{% endblock %}
{% block content %}
<center>
<h1>Welcome to Lex's Database of Favorite Books and Authors!</h1>
<p>I don't really enjoy reading but if I were to do so, action or rom-com would be the genres I will go to.</p>
<br>
<a href="/bookshelf/books">Books</a>
<a href="/bookshelf/authors">Authors</a>
</center>
{% 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,
BooksListView,
BooksDetailView,
AuthorsListView,
AuthorsDetailView
)
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('home/', index, name='homepage'),
path('books/', BooksListView.as_view(), name='books'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='book_details'),
path('authors/', AuthorsListView.as_view(), name='authors'),
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.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from django.views import View
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Author, Books
def index(request): def index(request):
return HttpResponse("") return render(request, 'bookshelf/home.html')
\ No newline at end of file
class BooksListView(ListView):
model = Books
template_name = 'bookshelf/books.html'
class BooksDetailView(DetailView):
model = Books
template_name = 'bookshelf/book_details.html'
class AuthorsListView(ListView):
model = Author
template_name = 'bookshelf/authors.html'
class AuthorsDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
\ No newline at end of file
...@@ -59,7 +59,7 @@ ROOT_URLCONF = 'lexchan_reading.urls' ...@@ -59,7 +59,7 @@ ROOT_URLCONF = 'lexchan_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>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
</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