Commit 4055c9e8 authored by Alvin Joshua Andrada's avatar Alvin Joshua Andrada

implemented templates, views, and urls

parent ae28f39b
Pipeline #3094 failed with stages
...@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ ...@@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
from pathlib import Path from pathlib import Path
from dotenv import load_dotenv from dotenv import load_dotenv
import os
load_dotenv() load_dotenv()
...@@ -23,8 +24,8 @@ BASE_DIR = Path(__file__).resolve().parent.parent ...@@ -23,8 +24,8 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-nu=92gz+p@&+26wa-kz7)v_p=uzeknm&6r9ofy1#ydsy30sv4^' # SECRET_KEY = 'django-insecure-nu=92gz+p@&+26wa-kz7)v_p=uzeknm&6r9ofy1#ydsy30sv4^'
# SECRET_KEY = os.getenv('SECRET_KEY') SECRET_KEY = os.getenv('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = True
...@@ -59,7 +60,7 @@ ROOT_URLCONF = 'andrada_reading.urls' ...@@ -59,7 +60,7 @@ ROOT_URLCONF = 'andrada_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': [
...@@ -120,7 +121,9 @@ USE_TZ = True ...@@ -120,7 +121,9 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/ # 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 # Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
......
...@@ -16,7 +16,8 @@ Including another URLconf ...@@ -16,7 +16,8 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('bookshelf/', include('bookshelf.urls', namespace="bookshelf")), path('home/', include('bookshelf.urls', namespace="bookshelf")),
] ]
from django.db import models from django.db import models
from django.urls import reverse
# Create your models here. # Create your models here.
class Author(models.Model): class Author(models.Model):
...@@ -10,6 +11,8 @@ class Author(models.Model): ...@@ -10,6 +11,8 @@ 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('author-detail', args=[str(self.id)])
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
...@@ -21,4 +24,5 @@ class Books(models.Model): ...@@ -21,4 +24,5 @@ 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('book-detail', args=[str(self.id)])
<!-- author-details -->
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h1>{{ author.first_name }} {{ author.last_name }}</h1>
<h4>{{ author.age }}</h4>
<h4>{{ author.nationality }}</h4>
<h4>{{ author.bio }}</h4>
<h4>Books by {{ author.first_name }} {{ author.last_name }} I love:</h4>
<ul>
{% for book in books_list %}
<li><a href="{% url 'bookshelf:book-detail' book.id %}">{{ book.title }}</a></li>
{% endfor %}
</ul>
<a href="/home/">Home</a>
<a href="/home/books/">Books</a>
<a href="/home/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
<!-- author html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h1>Alvin's Favorite Authors</h1>
<ul>
{% for author in author_list %}
<li><a href="{% url 'bookshelf:author-detail' author.id %}">{{ author.first_name }} {{ author.last_name}}</a></li>
{% endfor %}
</ul>
<a href="/home/">Home</a>
<a href="/home/books/">Books</a>
{% endblock %}
\ No newline at end of file
<!-- book_details -->
<!-- books html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<h3><a href="{% url 'bookshelf:author-detail' object.author.id %}">{{ object.author }}</a></h3>
<h3>{{ object.publisher }}</h3>
<h3>{{ object.year_published }}</h3>
<h3>{{ object.ISBN }}</h3>
<h3>{{ object.blurb }}</h3>
<a href="/home/">Home</a>
<a href="/home/books/">Books</a>
<a href="/home/authors/">Authors</a>
{% endblock %}
<!-- books html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h1>Alvin's Favorite Books</h1>
<ul>
{% for book in books_list %}
<li><a href="{% url 'bookshelf:book-detail' book.id %}">{{ book.title }}</a></li>
{% endfor %}
</ul>
<a href="/home/">Home</a>
<a href="/home/authors/">Authors</a>
{% endblock %}
<!-- home html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books & Authors{% endblock %}
{% block content %}
<h1>Welcome to Alvin's Database of Favorite Books and Authors!</h1>
<p>I usually read web fiction. The genres are similar to what you see
in animes. I mostly read LitRPGs and Chinese genres. I often read in a
particular website wherein amateur writers upload this kind of work. Many
of them are actually good.</p>
<a href="authors/">Authors</a>
<a href="books/">Books</a>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from . import views
from .views import BooksView, AuthorsView, BookDetailView, AuthorDetailView
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', views.home_view, name='home'),
path('books/', BooksView.as_view(), name='book-list'),
path('authors/', AuthorsView.as_view(), name='author-list'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'),
path('authors/<int:author_id>/details/', AuthorDetailView.as_view(), name='author-detail'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
# Bookshelf # Bookshelf
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.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 BooksView(ListView):
model = Books
template_name = 'bookshelf/books.html'
class BookDetailView(DetailView):
model = Books
template_name = 'bookshelf/book_details.html'
class AuthorsView(ListView):
model = Author
template_name = 'bookshelf/authors.html'
class AuthorDetailView(View):
def get(self, request, author_id):
author = Author.objects.get(pk=author_id)
books_list= Books.objects.all().filter(author=author)
return render(request, "bookshelf/author_details.html", {"author":author,"books_list":books_list})
\ No newline at end of file
<!-- base html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>{% block title %}{% 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