Commit 713e190e authored by Nics De Vega's avatar Nics De Vega

added template views and urls, minor fixes in bookshelf models

parent 6dbbe2d7
from django.db import models
from django.core.validators import MinValueValidator,MaxValueValidator, MinLengthValidator
from django.urls import reverse
class Author(models.Model):
first_name = models.CharField(max_length=100,default="")
......@@ -10,14 +11,23 @@ class Author(models.Model):
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return reverse("bookshelf:author-details",kwargs={'pk':self.pk})
class Book(models.Model):
title = models.CharField(max_length=100,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=100, default="")
year_published = models.PositiveIntegerField(validators=[MaxValueValidator(2023)])
ISBN = models.IntegerField(unique=True, validators=[MaxValueValidator(9999999999999),MinValueValidator(1000000000000)])
blurb = models.TextField(validators=[MinLengthValidator(100)],max_length=700,default ="")
def __str__(self):
return '{} by {}'.format(self.title, self.author)
\ No newline at end of file
return '{} by {}'.format(self.title, self.author)
def get_absolute_url(self):
return reverse("bookshelf:book-details",kwargs={'pk':self.pk})
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{object.first_name}} {{object.last_name}}{% endblock %}
{% block content %}
<h3>{{object.first_name}} {{object.last_name}}</h3>
<h4>{{object.age}}</h4>
<h4>{{object.nationality}}</h4>
<p>{{object.bio}}</p>
<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>
<a href="/bookshelf/home/">Home</a>
<a href="/bookshelf/books/">Books</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<h3>Welcome to Nics' Favorite Authors:</h3>
<ul>
{% for object in object_list %}
<li>
<a href="{{ object.get_absolute_url }}"> {{object.first_name}} {{object.last_name}} </a>
</li>
{% endfor %}
</ul>
<a href="/bookshelf/home/">Home</a>
<a href="/bookshelf/books/">Books</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{object.title}}{% endblock %}
{% block content %}
<h3>{{object.title}}</h3>
<a href ="{{ object.author.get_absolute_url }}"><h4>{{object.author}}</h4></a>
<h4>{{object.publisher}}</h4>
<h4>{{object.year_published}}</h4>
<h4>{{object.ISBN}}</h4>
<p>{{object.blurb}}</p>
<a href="/bookshelf/home/">Home</a>
<a href="/bookshelf/books/">Books</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h3>Welcome to Nics' Favorite Books:</h3>
<ul>
{% for object in object_list %}
<li>
<a href="{{ object.get_absolute_url }}">{{object.title}}</a>
</li>
{% endfor %}
</ul>
<a href="/bookshelf/home/">Home</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h2>Welcome to Nics' Database of Favorite Books and Authors!</h2>
<p>I usually like books with religious themes that make me go <b>Insane</b>. I also like Revue Starlight!</p>
<a href="/bookshelf/books/">Books</a>
<a href="/bookshelf/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
from django.urls import path
from .views import index
from .views import homepage, index_view, BooksView, AuthorsView, BooksDetailView, AuthorsDetailView
urlpatterns = [
path('',index,name='index')
path('',index_view,name='index'),
path('home/',homepage,name='home'),
path('books/',BooksView.as_view(),name='books'),
path('authors/',AuthorsView.as_view(),name='authors'),
path('books/<int:pk>/details/',BooksDetailView.as_view(),name='book-details'),
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.http import HttpResponse
from .models import Author, Book
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
def index(request):
return HttpResponse("Hello World! This is my bookshelf!")
\ No newline at end of file
def index_view(request):
return HttpResponse("<h3>Hello Welcome to My Bookshelf! Please go to another page lmao.</h3>")
def homepage(request):
return render(request, 'bookshelf/home.html', {'name': 'homepage'})
class BooksView(ListView):
model = Book
template_name = "bookshelf/books.html"
class BooksDetailView(DetailView):
model = Book
template_name = "bookshelf/book_details.html"
class AuthorsView(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 = 'nicsdevega_reading.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
......
......@@ -18,5 +18,5 @@ from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('bookshelf', include('bookshelf.urls', namespace="bookshelf")),
path('bookshelf/', include('bookshelf.urls', namespace="bookshelf")),
]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<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>
\ 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