Commit a91fcf1e authored by EJ Mejilla's avatar EJ Mejilla

Created the templates for all the required urls

parent 39a03f63
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=200, blank=True) first_name = models.CharField(max_length=200, blank=True)
...@@ -10,6 +11,9 @@ class Author(models.Model): ...@@ -10,6 +11,9 @@ class Author(models.Model):
def __str__(self): def __str__(self):
return f"{self.first_name} {self.last_name}" return f"{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=200, blank=True) title = models.CharField(max_length=200, blank=True)
...@@ -18,3 +22,9 @@ class Books(models.Model): ...@@ -18,3 +22,9 @@ class Books(models.Model):
year_published = models.IntegerField(default=0) year_published = models.IntegerField(default=0)
ISBN = models.IntegerField(default=0) ISBN = models.IntegerField(default=0)
blurb = models.TextField(max_length=1000, blank=True) blurb = models.TextField(max_length=1000, blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('bookshelf:book-details', kwargs={'pk': self.pk})
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ object.first_name }} {{object.last_name}} {% endblock %}
{% block content %}
<div class="details">
<h1> {{ object.first_name }} {{object.last_name}} </h1>
<ul>
<li>{{ object.age }}</li>
<li>{{ object.nationality }}</li>
<li>{{ object.bio }}</li>
</ul>
</div>
<div class="details">
<h2>Books by {{object.first_name}} {{object.last_name}} I love:</h2>
<ul>
{% for books in author.books_set.all %}
<li>
<a href="{{books.get_absolute_url}}">
{{ books.title }}
</a>
</li>
{% endfor %}
</ul>
</div>
<ul class="footer">
<li><a href="{% url 'bookshelf:home' %}">Home</a></li>
<li><a href="{% url 'bookshelf:authors' %}">Authors</a></li>
<li><a href="{% url 'bookshelf:books' %}">Books</a></li>
</ul>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} My Favorite Authors {% endblock %}
{% block content %}
<h1>EJ's Favorite Authors:</h1>
<ul class="list">
{% for object in object_list %}
<li>
<a href="{{object.get_absolute_url}}">
{{object.first_name}} {{object.last_name}}
</a>
</li>
{% endfor %}
</ul>
<ul class="footer">
<li><a href="{% url 'bookshelf:home' %}">Home</a></li>
<li><a href="{% url 'bookshelf:books' %}">Books</a></li>
</ul>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ object.title }} {% endblock %}
{% block content %}
<div class="details">
<h1>{{ object.title }}</h1>
<ul>
<li><a href="{{ object.author.get_absolute_url }}">{{object.author}}</a></li>
<li>{{ object.publisher }}</li>
<li>{{ object.year_published }}</li>
<li>{{ object.ISBN }}</li>
<li>{{ object.blurb }}</li>
</ul>
</div>
<ul class="footer">
<li><a href="{% url 'bookshelf:home' %}">Home</a></li>
<li><a href="{% url 'bookshelf:authors' %}">Authors</a></li>
<li><a href="{% url 'bookshelf:books' %}">Books</a></li>
</ul>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} My Favorite Books {% endblock %}
{% block content %}
<div class="list">
<h1>EJ's Favorite Books:</h1>
<ul>
{% for object in object_list %}
<li>
<a href="{{object.get_absolute_url}}">
{{object.title}}
</a>
</li>
{% endfor %}
</ul>
</div>
<ul class="footer">
<li><a href="{% url 'bookshelf:home' %}">Home</a></li>
<li><a href="{% url 'bookshelf:authors' %}">Authors</a></li>
</ul>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} My Favorite Books & Authors {% endblock %}
{% block content %}
<div class="homepage">
<h1>Welcome to EJ's Database of Favorite Books and Authors!</h1>
<p>I like a variety of books from YA novels to psychological horror :D</p>
</div>
<ul class="footer">
<li><a href="{% url 'bookshelf:books' %}">Books</a></li>
<li><a href="{% url 'bookshelf:authors' %}">Authors</a></li>
</ul>
{% endblock %}
# bookshelf/urls.py # bookshelf/urls.py
from django.urls import path from django.urls import path
from .views import index from .views import home_view, BookDetailView, BookListView, AuthorDetailView, AuthorListView
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', home_view, name='home'),
path('books/', BookListView.as_view(template_name='bookshelf/books.html'), name='books'),
path('books/<int:pk>/details/', BookDetailView.as_view(template_name="bookshelf/book_details.html"), name='book-details'),
path('authors/', AuthorListView.as_view(template_name='bookshelf/authors.html'), name='authors'),
path('authors/<int:pk>/details/', AuthorDetailView.as_view(template_name='bookshelf/author_details.html'), name='author-details'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
# bookshelf/views.py # appname/views.py
from django.http import HttpResponse from django.shortcuts import render
from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
def index(request): from .models import Author, Books
return HttpResponse('Hello World! This came from the index view')
def home_view(request):
return render(request, 'bookshelf/home.html')
class BookDetailView(DetailView):
model = Books
class BookListView(ListView):
model = Books
class AuthorDetailView(DetailView):
model = Author
class AuthorListView(ListView):
model = Author
...@@ -57,7 +57,7 @@ ROOT_URLCONF = 'ejmejilla_reading.urls' ...@@ -57,7 +57,7 @@ ROOT_URLCONF = 'ejmejilla_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': [
......
...@@ -18,5 +18,5 @@ from django.urls import include, path ...@@ -18,5 +18,5 @@ from django.urls import include, path
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('bookshelf/', include('bookshelf.urls', namespace="bookshelf")) path('bookshelf/', include('bookshelf.urls')),
] ]
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>{% block title %}My amazing site{% endblock %}</title>
{% block styles %}{% endblock %}
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
{% block scripts %}{% endblock %}
</body>
</html>
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