Assignment 4 Q2
Assignment 4 Q2
Q2. For the database Furniture mentioned in the previous question, create ListView
that displays list of customers with furniture names they have purchased. On
clicking furniture, furniture details should be displayed.
Solution
Views.py
from django.shortcuts import render
class CustomerListView(generic.ListView):
model = Customer
template_name = "customer_list.html"
class FurnitureDetailView(generic.DetailView):
model = Furniture
template_name = "furniture_detail.html"
customer_list.html
<html>
<head>
<title>Customer List</title>
</head>
<body>
<h1>Customers and Their Purchased Furniture</h1>
<ul>
{% for customer in customer_list %}
<li>{{ customer.customername }}</li>
<ul>
{% for furniture in customer.purchase.all %}
<li><a href= "/furniture_detail/ {{furniture.pk}}" >
{{furniture.furniture_name}} </a></li>
{% endfor %}
</ul>
{% endfor %}
</ul>
</body>
</html>
Furniture_detail.html
<html>
<head>
<title>Furniture Detail</title>
</head>
<body>
<h1>Furniture{{ furniture.furniture_name }}</h1>
<p>Type: {{ furniture.furniture_type }} </p>
<p>Features: {{ furniture.features }}</p>
<p>Price: {{ furniture.price }}</p>
<p>Material Used: {{ furniture.material_used }}</p>
</body>
</html>
Urls.py
from assign4.views import CustomerListView, FurnitureDetailView
urlpatterns = [
path('customers/', CustomerListView.as_view()),
path('furniture/<int:pk>/', FurnitureDetailView.as_view()),
Output