Skip to content

3. Example - URL Reversal

1. Update the View to Use a Template

Modify views.py so that it renders a template instead of returning plain text.

View for ID-based Book Detail

from django.shortcuts import render, get_object_or_404
from .models import Book # Import your Book model
def book_detail(request, book_id):
book = get_object_or_404(Book, id=book_id) # Fetch book by ID
return render(request, 'book_detail.html', {'book': book})

View for Slug-based Book Detail

def book_detail_by_slug(request, book_slug):
book = get_object_or_404(Book, slug=book_slug) # Fetch book by slug
return render(request, 'book_detail.html', {'book': book})

2. Create a Django Template (book_detail.html)

Inside your app’s templates folder, create a file named book_detail.html.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ book.title }}</title>
</head>
<body>
<h1>{{ book.title }}</h1>
<p><strong>Author:</strong> {{ book.author }}</p>
<p><strong>Published Date:</strong> {{ book.published_date }}</p>
<p><strong>Description:</strong> {{ book.description }}</p>
<a href="{% url 'book_list' %}">Back to Books</a>
</body>
</html>

3. Define the Book Model

If you haven’t already created a Book model, define it in models.py:

from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
description = models.TextField()
published_date = models.DateField()
def __str__(self):
return self.title

Run the following commands to apply the model changes:

Terminal window
python manage.py makemigrations
python manage.py migrate

4. Create a Book List Page

To display a list of books with links to their details, create another view and template.

View for Listing Books (views.py)

def book_list(request):
books = Book.objects.all()
return render(request, 'book_list.html', {'books': books})

URL Configuration (urls.py)

urlpatterns = [
path('books/', views.book_list, name='book_list'), # List all books
path('book/<int:book_id>/', views.book_detail, name='book_detail'), # View book by ID
path('book/<slug:book_slug>/', views.book_detail_by_slug, name='book_detail_by_slug'), # View book by slug
]

Template for Listing Books (book_list.html)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book List</title>
</head>
<body>
<h1>Book List</h1>
<ul>
{% for book in books %}
<li>
<a href="{% url 'book_detail' book.id %}">{{ book.title }}</a>
- <a href="{% url 'book_detail_by_slug' book.slug %}">(View by Slug)</a>
</li>
{% endfor %}
</ul>
</body>
</html>

5. Running the Project

Start your Django server:

Terminal window
python manage.py runserver

Now, open your browser and visit:

  • Book List: http://127.0.0.1:8000/books/
  • Book Detail by ID: http://127.0.0.1:8000/book/1/
  • Book Detail by Slug: http://127.0.0.1:8000/book/django-for-beginners/

Summary

✔ Defined URL patterns for book details using ID and slug
✔ Created a view to fetch and display book details
✔ Designed templates to show book details and book lists
✔ Implemented a Book model to store book information