Django Simple Coding Notesbook View
Django Simple Coding Notesbook View
author, title, and description. We will then display the list of books in an HTML page using a for
loop.
Steps:
De ne the Book model with elds for name, author, title, and description.
class Book(models.Model):
book_name = models.CharField(max_length=255)
author = models.CharField(max_length=255)
title = models.CharField(max_length=255)
description = models.TextField()
def __str__(self):
return self.title
Run the following commands to create the database table for the Book model:
urlpatterns = [
path('books/', views.book_list, name='book_list'),
]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book List</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<li class="list-group-item">
<h5>{{ book.title }}</h5>
</li>
{% empty %}
{% endfor %}
</ul>
</div>
</body>
</html>
You can use Django's admin panel or the Django shell to add sample book data. To do this via the
shell: