Commit 59ef92c9 authored by Ilham Maulana's avatar Ilham Maulana 💻

feat: book list in category detail

parent 40b62c33
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
<tr class="table-primary"> <tr class="table-primary">
<th scope="col">Title</th> <th scope="col">Title</th>
<th scope="col">Category</th> <th scope="col">Category</th>
<th scope="col">Description</th>
<th scope="col">Published Date</th> <th scope="col">Published Date</th>
<th scope="col">Created At</th> <th scope="col">Created At</th>
<th scope="col">Updated At</th> <th scope="col">Updated At</th>
...@@ -15,7 +14,6 @@ ...@@ -15,7 +14,6 @@
<tr> <tr>
<td><a href="/books/{{ book.id }}/">{{ book.title }}</a></td> <td><a href="/books/{{ book.id }}/">{{ book.title }}</a></td>
<td>{{ book.category.name }}</td> <td>{{ book.category.name }}</td>
<td>{{ book.description }}</td>
<td>{{ book.publish_date }}</td> <td>{{ book.publish_date }}</td>
<td>{{ book.created_at }}</td> <td>{{ book.created_at }}</td>
<td>{{ book.updated_at }}</td> <td>{{ book.updated_at }}</td>
......
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
<tbody> <tbody>
{% if object_list %} {% for category in object_list %} {% if object_list %} {% for category in object_list %}
<tr> <tr>
<td>{{ category.name }}</td> <td><a href="/books/categories/{{ category.id }}/">{{ category.name }}</a></td>
<td>{{ category.created_at }}</td> <td>{{ category.created_at }}</td>
<td>{{ category.updated_at }}</td> <td>{{ category.updated_at }}</td>
<td class="d-flex gap-2"> <td class="d-flex gap-2">
<a <a
class="btn btn-success" class="btn btn-success"
href="/books/categories/{{ category.id }}/" href="/books/categories/{{ category.id }}/update/"
> >
<i class="bi bi-pencil-square"></i> <i class="bi bi-pencil-square"></i>
</a> </a>
......
{% extends "layout.html" %} {% block dashboard %}
<div style="max-width: 80vw" class="w-100 p-4">
<div class="d-flex justify-content-between pb-4">
<div class="d-flex gap-2 pb-4">
<a class="btn btn-success" href="/books/categories/{{ category.id }}/update">
<i class="bi bi-pencil-square"></i> Edit
</a>
<a class="btn btn-danger" href="/books/categories/{{ category.id }}/delete/">
<i class="bi bi-trash3-fill"></i> Delete
</a>
</div>
</div>
<div class="d-flex gap-5">
<div class="col">
<h1 class="h2 row">{{ category.name }}</h1>
<time datetime="{{ category.created_at }}" class="row fs-6"
>Created at: {{ category.created_at }}</time
>
<time datetime="{{ category.updated_at }}" class="row fs-6"
>Updated at: {{ category.updated_at }}</time
>
<h2 class="h3 row mt-4">List book with category {{ category.name }}</h2>
{% include "book_table_data.html" %}
</div>
</div>
{% endblock dashboard %}
\ No newline at end of file
...@@ -6,6 +6,7 @@ from .views import ( ...@@ -6,6 +6,7 @@ from .views import (
BookUpdateView, BookUpdateView,
BookDeleteView, BookDeleteView,
CategoryListView, CategoryListView,
CategoryDetailView,
CategoryCreateView, CategoryCreateView,
CategoryUpdateView, CategoryUpdateView,
CategoryDeleteView, CategoryDeleteView,
...@@ -19,7 +20,16 @@ urlpatterns = [ ...@@ -19,7 +20,16 @@ urlpatterns = [
path("<int:pk>/delete/", BookDeleteView.as_view(), name="book_delete"), path("<int:pk>/delete/", BookDeleteView.as_view(), name="book_delete"),
path("categories/", CategoryListView.as_view(), name="category_list"), path("categories/", CategoryListView.as_view(), name="category_list"),
path("categories/add/", CategoryCreateView.as_view(), name="category_update"), path("categories/add/", CategoryCreateView.as_view(), name="category_update"),
path("categories/<int:pk>/", CategoryUpdateView.as_view(), name="category_update"), path(
"categories/<int:pk>/",
CategoryDetailView.as_view(),
name="category_detail",
),
path(
"categories/<int:pk>/update/",
CategoryUpdateView.as_view(),
name="category_update",
),
path( path(
"categories/<int:pk>/delete/", "categories/<int:pk>/delete/",
CategoryDeleteView.as_view(), CategoryDeleteView.as_view(),
......
...@@ -80,6 +80,25 @@ class CategoryListView(generic.ListView): ...@@ -80,6 +80,25 @@ class CategoryListView(generic.ListView):
return queryset.order_by("-updated_at") return queryset.order_by("-updated_at")
class CategoryDetailView(generic.DetailView):
model = Category
template_name = "category_detail.html"
context_object_name = "category"
def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context["object"] = self.object
context_object_name = self.get_context_object_name(self.object)
books = Book.objects.filter(category=self.object.id)
context["object_list"] = books
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
class CategoryCreateView(generic.edit.CreateView): class CategoryCreateView(generic.edit.CreateView):
model = Category model = Category
form_class = CategoryForm form_class = CategoryForm
......
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