I am trying to fix url guarding issue while using DetailView in django web for displaying a particular object (product) details and passing id (pk) to url that doesn't exist in the product model (table) instances. currently it shows Type Error like "No products found matching the query" . instead i am looking for a way to display caution page to the user that the required product id doesn't exist.
Illustrated as sample below
models.py
from django.db import models
class Products(models.Model):
product_name=models.CharField(max_length=200)
product_descript=models.CharField(max_length=200)
product_price=models.IntegerField()
def __str__(self):
return self.product_name
url.py
from django.urls import path
from . import views
urlpatterns=[ path('product/detail=<int:pk>',views.ProductDetailView.as_view(),
name='detail'),]
views.py
from django.views.generic.detail import DetailView
from .models import Products
class ProductDetailView(DetailView):
model=Products
template_name='food/detail.html'
food/detail.html
<div>
<h1>Name: {{object.product_name}}</h1>
<h3>Descrription: {{object.product_descript}}</h3>
<h4>Price: {{object.product_price}}</h4>
</div>
Detail page while passing not exist id.