0

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.

Error messgae

  • When the DEBUG setting is False your app will return a standard 404 response which is pretty much what you are asking for? Or do you want to customised this 404 response? – Iain Shelvington Jan 01 '22 at 15:00
  • actually the thing which i am looking for, to add customized response page. thanks & regards – rafee muhammed Jan 01 '22 at 15:41
  • Does this answer your question? [Django, creating a custom 500/404 error page](https://stackoverflow.com/questions/17662928/django-creating-a-custom-500-404-error-page) – Ankit Tiwari Jan 01 '22 at 16:25

0 Answers0