Commit be6c8b2a authored by Angelo Alvarez's avatar Angelo Alvarez

Applied DetailView to Model Books

parent 9fbb8919
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):
...@@ -20,4 +21,7 @@ class Books(models.Model): ...@@ -20,4 +21,7 @@ class Books(models.Model):
blurb = models.TextField(max_length=1000) blurb = models.TextField(max_length=1000)
def __str__(self): def __str__(self):
return self.title return self.title
\ No newline at end of file
def get_absolute_url(self):
return reverse('bookshelf:books-details', kwargs={'pk': self.pk})
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ object.title }}{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<em>Author: </em><a href="{{ object.author.get_absolute_url }}">{{ object.author }}</a> <br>
<em>Publisher: </em>{{ object.publisher }} <br>
<em>year_published: </em>{{ object.year_published }} <br>
<em>ISBN: </em>{{ object.ISBN }} <br>
{{ object.blurb }}
<hr>
<div id="links" style="margin: auto; text-align: center; width: 100%">
<a href="../home/">Home</a>
&nbsp; &nbsp;
<a href="../books/">Books</a>
&nbsp; &nbsp;
<a href="../authors/">Authors</a>
</div>
{% endblock %}
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
{% block content %} {% block content %}
<h1>Alva's Favorite Books</h1> <h1>Alva's Favorite Books</h1>
<ul> <ul>
{% for book in books %} {% for object in object_list %}
<li><a href="{{ book.get_absolute_url }}">{{ book.title }}</a></li> <li><a href="{{ object.get_absolute_url }}">{{ object.title }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
......
from django.urls import path from django.urls import path
from .views import home, BooksListView from .views import home, BooksListView, BooksDetailView
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
path('books/', BooksListView.as_view(), name='books') path('books/', BooksListView.as_view(), name='books-list'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-details')
] ]
# This might be needed, depending on your Django version # This might be needed, depending on your Django version
......
...@@ -7,11 +7,11 @@ from .models import Books ...@@ -7,11 +7,11 @@ from .models import Books
# Create your views here. # Create your views here.
def home(request): def home(request):
return render(request, 'events/home.html') return render(request, 'home.html')
class BooksListView(View): class BooksListView(ListView):
model = Books model = Books
def get(self, request):
booklist = Books.objects.all() class BooksDetailView(DetailView):
return render(request, 'events/books.html', {'books' : booklist}) model = Books
\ No newline at end of file \ 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