Create templates and update views and urls

Created base and home templates and listview and detailview of both authors and books and fixed their urls and hyperlinks
parent 5c1ea638
# Generated by Django 4.1.7 on 2023-03-28 13:59
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='books',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='books', to='bookshelf.author'),
),
]
from django.db import models
from django.urls import reverse
# Create your models here.
class Author(models.Model):
......@@ -13,7 +13,7 @@ class Author(models.Model):
return '{} {}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return f"{self.pk}"
return reverse('bookshelf:author-detail', kwargs={'pk': self.pk})
class Books(models.Model):
......@@ -28,4 +28,4 @@ class Books(models.Model):
return '{}'.format(self.title)
def get_absolute_url(self):
return f"{self.pk}"
\ No newline at end of file
return reverse('bookshelf:books-detail', kwargs={'pk': self.pk})
\ No newline at end of file
......@@ -6,8 +6,8 @@
</head>
<body>
<div id="content">
{% block content%}{% endblock %}
{% block content%} {% endblock %}
</div>
{% block scripts %}{% endblock %}
{% block scripts %} {% endblock %}
</body>
</html>
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} {{ object.first_name }} {{ object.last_name }} {% endblock %}
{% block content %}
<h1> {{ object.first_name }} {{ object.last_name }} </h1>
<h3> Age: {{ object.age }} </h3>
<h3> Nationality: {{ object.nationality }} </h3>
<h3> About the Author: </h3> <p> {{ object.bio }} </p>
Books by {{ object.first_name }} {{ object.last_name }} I love:
<ul>
{% for book in object.books.all%}
<li>
<a href='../../../..{{ book.get_absolute_url }}'>
{{ book.title }}
</a>
{% endfor %}
</li>
</ul>
<a href="/home">Home</a>
<a href="/books">Books</a>
<a href="/authors">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} My Favorite Authors {% endblock %}
{% block content %}
<h1> Lay's Favorite Authors:</h1>
<ul>
{% for object in object_list%}
<li>
<a href='{{ object.get_absolute_url }}'>
{{ object.first_name }} {{ object.last_name}}
</a>
{% endfor %}
</li>
</ul>
<a href="/home">Home</a>
<a href="/books">Books</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} {{ object.title }} {% endblock %}
{% block content %}
<h1> {{ object.title }} </h1>
<h2><a href="../../..{{ object.author.get_absolute_url }}"> by {{ object.author }}</a></h2>
<h3> Published by: {{ object.publisher }} </h3>
<h3> Published on: {{ object.year_published }} </h3>
<h3> ISBN: {{ object.ISBN }}</h3>
<h3> Blurb: </h3> <p> {{ object.blurb }} </p>
<a href="/home">Home</a>
<a href="/books">Books</a>
<a href="/authors">Authors</a>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} My Favorite Books {% endblock %}
{% block content %}
<h1> Lay's Favorite Books:</h1>
<ul>
{% for object in object_list%}
<li>
<a href='{{ object.get_absolute_url }}'>
{{ object.title }}
</a>
{% endfor %}
</li>
</ul>
<a href="/books">Books</a>
<a href="/authors">Authors</a>
{% endblock %}
\ No newline at end of file
from django.urls import path
from .views import index
from .views import home, BooksListView, BooksDetailView, AuthorListView, AuthorDetailView
urlpatterns = [
path('', index, name='index'),
path('home/', home, name='home'),
path('books/', BooksListView.as_view(), name='books-list'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-detail'),
path('authors/', AuthorListView.as_view(), name='authors-list'),
path('authors/<int:pk>/details/', AuthorDetailView.as_view(), name='author-detail')
]
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 .models import Author, Books
def index(request):
return render(request, 'home.html', {'name': 'Lay'})
\ No newline at end of file
def home(request):
return render(request, 'home.html', {'name': 'Lay'})
class BooksListView(ListView):
model = Books
template_name = "bookshelf/books.html"
class BooksDetailView(DetailView):
model = Books
template_name = "bookshelf/book_detais.html"
class AuthorListView(ListView):
model = Author
template_name = "bookshelf/authors.html"
class AuthorDetailView(DetailView):
model = Author
template_name = "bookshelf/author_details.html"
\ No newline at end of file
......@@ -17,6 +17,7 @@ from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('bookshelf/', include('bookshelf.urls', namespace="bookshelf")),
path('', include('bookshelf.urls', namespace="bookshelf")),
path('admin/', admin.site.urls),
]
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