Implemented a homepage view at home/ and a books view at books/

parent 121689ab
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=150) first_name = models.CharField(max_length=150)
...@@ -10,6 +11,10 @@ class Author(models.Model): ...@@ -10,6 +11,10 @@ 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('bookshelf:author-detail', kwargs={'pk': self.pk})
class Book(models.Model): class Book(models.Model):
title = models.CharField(max_length=180) title = models.CharField(max_length=180)
author = models.ForeignKey(Author, on_delete=models.CASCADE) author = models.ForeignKey(Author, on_delete=models.CASCADE)
...@@ -20,3 +25,6 @@ class Book(models.Model): ...@@ -20,3 +25,6 @@ class Book(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse('bookshelf:book-detail', kwargs={'pk': self.pk})
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h1>Matt's Favorite Books:<br><br></h1>
<ul style="list-style-type:circle">
{% for book in books %}
<li><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></li>
{% endfor %}
</ul>
<a href="http://localhost:8000/home/">Home</a>
<a href="http://localhost:8000/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<h1>Matt's Favorite Books:<br><br></h1>
<ul style="list-style-type:circle">
{% for book in books %}
<li><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></li>
{% endfor %}
</ul>
<a href="http://localhost:8000/home/">Home</a>
<a href="http://localhost:8000/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books & Authors{% endblock %}
{% block content %}
<h1>Welcome to Matt's Database of Favorite Books and Authors!<br><br></h1>
<p>I like fantasy, sci-fi, and detective novels written by (perhaps) depressed and somewhat manic authors!<br></p>
<a href="http://localhost:8000/books/">Books</a>
<a href="http://localhost:8000/authors/">Authors</a>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import bookshelfpageview from .views import homepage, BooksView, BookDetailView
urlpatterns = [ urlpatterns = [
path('', bookshelfpageview, name='bookshelf') path('home/', homepage, name='homepage'),
path('books/', BooksView.as_view(), name='book-list'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail')
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from .models import Book, Author
def bookshelfpageview(request): def homepage(request):
return HttpResponse("Welcome to Matt's Music Library!") return render(request, 'bookshelf/home.html')
\ No newline at end of file
class BooksView(ListView):
def get(self, request):
books = Book.objects.all()
return render(request, 'bookshelf/books.html', {'books': books})
class BookDetailView(DetailView):
def get(self, request):
books = Book.objects.all()
return render(request, 'bookshelf/book_details.html', {'books': books})
class AuthorsView(ListView):
def get(self, request):
authors = Author.objects.all()
return render(request, 'bookshelf/authors.html', {'authors': authors})
class AuthorDetailView(DetailView):
def get(self, request):
authors = Author.objects.all()
return render(request, 'bookshelf/author_details.html', {'authors': authors})
\ No newline at end of file
...@@ -47,7 +47,7 @@ ROOT_URLCONF = 'mattarpas_reading.urls' ...@@ -47,7 +47,7 @@ ROOT_URLCONF = 'mattarpas_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': [
......
...@@ -2,6 +2,6 @@ from django.contrib import admin ...@@ -2,6 +2,6 @@ from django.contrib import admin
from django.urls import include, path from django.urls import include, path
urlpatterns = [ urlpatterns = [
path('bookshelf/', include('bookshelf.urls', namespace="bookshelf")), path('', include('bookshelf.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>{% block title %}Bookshelf{% 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