Commit da406426 authored by Paolo Luis R. Encisa's avatar Paolo Luis R. Encisa

added the bookshelf app

parents
Pipeline #2987 canceled with stages
from django.contrib import admin
from .models import Author, Books
class AuthorAdmin(admin.ModelAdmin):
model = Author
list_display = ('first_name', 'last_name', 'age', 'nationality')
search_fields = ('first_name', 'last_name', 'nationality')
list_filter = ('nationality',)
fields = ('first_name', 'last_name', 'age', 'nationality', 'bio')
class BookAdmin(admin.ModelAdmin):
model = Books
list_display = ('title', 'author', 'publisher', 'year_published', 'ISBN')
search_fields = ('title', 'author__first_name', 'author__last_name', 'publisher')
list_filter = ('year_published',)
fields = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb')
admin.site.register(Author, AuthorAdmin)
admin.site.register(Books, BookAdmin)
from django.apps import AppConfig
class BookshelfConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'bookshelf'
# Generated by Django 4.1.7 on 2023-03-27 14:34
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=100)),
('last_name', models.CharField(max_length=100)),
('age', models.IntegerField()),
('nationality', models.CharField(max_length=100)),
('bio', models.CharField(max_length=700)),
],
),
migrations.CreateModel(
name='Books',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('publisher', models.CharField(max_length=100)),
('year_published', models.IntegerField()),
('ISBN', models.CharField(max_length=13)),
('blurb', models.CharField(max_length=200)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookshelf.author')),
],
),
]
# Generated by Django 4.1.7 on 2023-03-27 18:25
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='books',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='books', to='bookshelf.author'),
),
]
from django.db import models
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
age = models.IntegerField()
nationality = models.CharField(max_length=100)
bio = models.CharField(max_length=700)
def __str__(self):
return self.first_name + " " + self.last_name
class Books(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')
publisher = models.CharField(max_length=100)
year_published = models.IntegerField()
ISBN = models.CharField(max_length=13)
blurb = models.CharField(max_length=200)
def __str__(self):
return self.title
{% extends 'bookshelf/base.html' %}
{% block title %}{{ author.first_name }} {{ author.last_name }}{% endblock %}
{% block content %}
<h1>{{ author.first_name }} {{ author.last_name }}</h1>
<p>Age: {{ author.age }}</p>
<p>Nationality: {{ author.nationality }}</p>
<p>{{ author.bio }}</p>
<h2>Books by {{ author.first_name }} {{ author.last_name }} I Love:</h2>
<ul>
{% for book in author.books.all %}
<li><a href="{% url 'book_details' book.pk %}">{{ book.title }}</a></li>
{% empty %}
<li>No books found for {{ author.first_name }} {{ author.last_name }}</li>
{% endfor %}
</ul>
<ul>
<li><a href="{% url 'home' %}">Home</a>&emsp; <a href="{% url 'books' %}">Books</a> &emsp; <a href="{% url 'authors' %}">Authors</a></li>
</ul>
{% endblock %}
{% extends 'bookshelf/base.html' %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h1>Pao's Favorite Authors</h1>
<ul>
{% for author in authors %}
<li><a href="{% url 'author_details' author.pk %}">{{ author.first_name }} {{ author.last_name }}</a></li>
{% endfor %}
</ul>
<ul>
<li><a href="{% url 'home' %}">Home</a>&emsp;<a href="{% url 'books' %}">Books</a></li>
</ul>
{% endblock %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
{% extends 'bookshelf/base.html' %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h2>Pao's Favorite Books:</h2>
<ul>
{% for book in books %}
<li><a href="{% url 'book_details' book.pk %}">{{ book.title }}</a></li>
{% endfor %}
</ul>
<nav>
<ul>
<li><a href="{% url 'home' %}">Home</a> &emsp;<a href="{% url 'authors' %}">Authors</li>
</ul>
</nav>
{% endblock %}
{% extends 'bookshelf/base.html' %}
{% block content %}
<h1>{{ book.title }}</h1>
<p><a href="{% url 'author_details' book.author.pk %}">{{ book.author }}</a></p>
<p>{{ book.publisher }}</p>
<p>{{ book.year_published }}</p>
<p>{{ book.ISBN }}</p>
<p>{{ book.blurb }}</p>
{% endblock %}
{% block sidebar %}
<h3>Navigation</h3>
<ul>
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'books' %}">Books</a></li>
<li><a href="{% url 'authors' %}">Authors</a></li>
</ul>
{% endblock %}
{% extends 'bookshelf/base.html' %}
{% block title %}My Favorite Books & Authors{% endblock %}
{% block content %}
<h1>Welcome to Pao's Database of Favorite Books and Authors!</h1>
<p>Greetings and a warm welcome to my Books Database! As an avid reader, I have a great passion for the fantasy genre, particularly works by literary titans such as J.K. Rowling and Rick Riordan.</p>
<ul>
<li><a href="{% url 'books' %}">Books</a> &emsp; <a href="{% url 'authors' %}">Authors</a></li>
</ul>
{% endblock %}
from django.test import TestCase
# Create your tests here.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('books/', views.BookListView.as_view(), name='books'),
path('books/<int:pk>/details/', views.BookDetailView.as_view(), name='book_details'),
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('authors/<int:pk>/', views.AuthorDetailView.as_view(), name='author_details'),
]
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Books, Author
class BookListView(ListView):
model = Books
template_name = 'bookshelf/books.html'
context_object_name = 'books'
class BookDetailView(DetailView):
model = Books
template_name = 'bookshelf/books_details.html'
context_object_name = 'book'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['book'] = self.get_object()
return context
class AuthorListView(ListView):
model = Author
template_name = 'bookshelf/authors.html'
context_object_name = 'authors'
class AuthorDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
context_object_name = 'author'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['author'] = self.get_object()
return context
def home(request):
context = {
'title': 'My Favorite Books & Authors',
'heading': "Welcome to Pao's Database of Favorite Books and Authors!",
'description': 'I Love Fantasy Books!',
'books_link': 'books/',
'authors_link': 'authors/'
}
return render(request, 'bookshelf/home.html', context=context)
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