Commit 1bb4abe7 authored by Mariam Garsin's avatar Mariam Garsin

Added the bookshelf and template files

parent 2207da7d
from django.contrib import admin
from.models import Author, Book
class AuthorAdmin(admin.ModelAdmin):
model = Author
class BooksAdmin(admin.ModelAdmin):
model = Book
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book, BooksAdmin)
\ No newline at end of file
from django.apps import AppConfig
class BookshelfConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'bookshelf'
# Generated by Django 4.2 on 2023-04-28 18:35
from django.db import migrations, models
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.TextField()),
],
),
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)),
('author', models.CharField(max_length=100)),
('publisher', models.CharField(max_length=100)),
('year_published', models.IntegerField()),
('ISBN', models.IntegerField()),
('blurb', models.TextField()),
],
),
]
# Generated by Django 4.2 on 2023-04-28 20:39
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.RenameModel(
old_name='Books',
new_name='Book',
),
]
# Generated by Django 4.2 on 2023-05-04 13:19
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0002_rename_books_book'),
]
operations = [
migrations.AlterField(
model_name='book',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookshelf.author'),
),
]
# Generated by Django 4.2 on 2023-05-08 15:08
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0003_alter_book_author'),
]
operations = [
migrations.AlterField(
model_name='book',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='author', to='bookshelf.author'),
),
]
# Generated by Django 4.2 on 2023-05-08 15:13
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0004_alter_book_author'),
]
operations = [
migrations.AlterField(
model_name='book',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookshelf.author'),
),
]
from django.db import models
from django.urls import reverse
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.TextField()
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return reverse('bookshelf:author_details', args=[self.pk])
def get_books(self):
return Book.objects.filter(author=self)
class Book(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.IntegerField()
blurb = models.TextField()
def __str__(self):
return "{} ({}, {})".format(self.title, self.author, self.year_published)
def get_absolute_url(self):
return reverse('bookshelf:book_details', args=[self.pk])
\ No newline at end of file
<!-- project/template/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>{% block title %}My site{% endblock %}</title>
{% block styles %}{% endblock %}
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
{% block scripts %}{% endblock %}
</body>
</html>
{% extends 'base.html' %}
{% load static %}
{% block title %} {{object}} {% endblock %}
{% block content %}
<h2>{{object}}</h2>
<div>
<p>Age: {{object.age}}</p>
<p>Nationality: {{object.nationality}}</p>
<p>Bio: {{object.bio}}</p>
</div>
<div>
<p>Books by {{object}} that I love:</p>
{% for book in object.books.all %}
<p><a href="{{book.get_absolute_url}}">
{{book.title}}
</a></p>
{% endfor %}
</div>
<a href="{% url 'bookshelf:home_view' %}">Home</a>
<a href="{% url 'bookshelf:books' %}">Books</a>
<a href="{% url '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>Yam's Database of Favorite Authors</h2>
<div>
{% for object in object_list %}
<p><a href = "{{object.get_absolute_url}}">
<li>{{object.first_name}} {{object.last_name}}</li>
</a></p>
{% endfor %}
</div>
<a href="{% url 'bookshelf:home_view' %}">Home</a>
<a href="{% url 'bookshelf:books' %}">Books</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} {{object.title}} {% endblock %}
{% block content %}
<h2>{{object.title}}</h2>
<p><a href="{{object.author.get_absolute_url}}">
by {{object.author}}
</a> </p>
<div>
<p>Published by {{object.publisher}} in {{object.year_published}}</p>
<p>ISBN: {{object.ISBN}}</p>
</div>
<div>
<p>{{object.blurb}}</p>
</div>
<a href="{% url 'bookshelf:home_view' %}">Home</a>
<a href="{% url 'bookshelf:books' %}">Books</a>
<a href="{% url '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 %}
<h2>Yam's Database of Favorite Books</h2>
<div>
{% for object in object_list %}
<p><a href = "{{object.get_absolute_url}}">
{{object.title}}
</a></p>
{% endfor %}
</div>
<a href="{% url 'bookshelf:home_view' %}">Home</a>
<a href="{% url '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 %}
<h2>Welcome to Yam's Database of Favorite Books and Authors</h2>
<div>
Hi! My name is Yam. I am into various genres of books. :>
</div>
<a href="{% url 'bookshelf:authors' %}">Authors</a>
<a href="{% url 'bookshelf:books' %}">Books</a>
{% endblock %}
from django.test import TestCase
# Create your tests here.
from django.contrib import admin
from django.urls import path
from .views import home_view, AuthorList, BookList, AuthorView, BookView
urlpatterns = [
path('home/',home_view, name = 'home_view'),
path('authors/', AuthorList.as_view(), name = 'authors'),
path('books/', BookList.as_view(), name = 'books'),
path('authors/<int:pk>/details/', AuthorView.as_view(), name = 'author_details'),
path('books/<int:pk>/details/', BookView.as_view(), name = 'book_details'),
]
app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.http import HttpResponse
from .models import Author, Book
def home_view(request):
return render(request,'bookshelf/home.html')
class AuthorList(ListView):
model = Author
template_name = 'bookshelf/authors.html'
class BookList(ListView):
model = Book
template_name = 'bookshelf/books.html'
class AuthorView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
class BookView(DetailView):
model = Book
template_name = 'bookshelf/book_details.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