I am building an Ecommerce site thru Django framework. While creating category section which takes user to individual category to access all its items, following error occurs.
Page not found (404)
No Category matches the given query.
Request Method: GET
Request URL: http://127.0.0.1:8000/search/ring/
Raised by: store.views.category_list
Using the URLconf defined in core.urls, Django tried these URL patterns, in this order:
admin/
[name='all_product']
item/<slug:slug>/ [name='product_detail']
search/<slug:category_slug>/ [name='category_list']
The current path, search/ring/, matched the last one.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Here is the urls.py file
from django.urls import path
from . import views
app_name = 'store'
urlpatterns = [
path('', views.all_product, name='all_product'),
path('item/<slug:slug>/', views.product_detail, name='product_detail'),
path('search/<slug:category_slug>/', views.category_list, name='category_list'),
]
views.py file
from django.shortcuts import get_object_or_404, render
from .models import Product, Category
# Create your views here.
def categories(request):
return {
'categories': Category.objects.all()
}
def all_product(request):
product = Product.objects.all()
return render(request, 'store/home.html', {'product': product})
def category_list(request, category_slug):
category = get_object_or_404(Category, slug=category_slug)
product = Product.objects.filter(category=category)
return render(request, 'store/products/category.html', {'category': category, 'product': product})
def product_detail(request, slug):
product = get_object_or_404(Product, slug=slug, in_stock=True)
return render(request, 'store/products/detail.html', {'product': product})
models.py file
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, unique=True)
class meta:
verbose_name_plural = 'categories'
def get_absolute_url(self):
return reverse('store:category_list', args=[self.slug])
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category, related_name='product' , on_delete= models.CASCADE)
created_by= models.ForeignKey(User, on_delete=models.CASCADE, related_name='product_creator' )
title = models.CharField(max_length=100)
author = models.CharField(max_length=100, default='admin')
description = models.TextField(blank=True)
image = models.ImageField(upload_to='images/')
slug = models.SlugField(max_length=100)
price = models.DecimalField(max_digits=4, decimal_places=2)
in_stock = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
class meta:
verbose_name_plural = 'products'
ordering = ('-created',)
def get_absolute_url(self):
return reverse('store:product_detail', args=[self.slug])
def __str__(self):
return self.title
And category.html file
{% extends "../base.html" %}
{% load static %}
{% block title %}
{% if category %}{{ category.name }}{% else %}Product{% endif %}
{% endblock %}
{% block content %}
<main>
<div class="album py-5 bg-light">
<div class="container">
<div class="pb-3 h5">{{category.name|title}}</div>
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-5 g-3">
{% for product in product %}
<div class="col">
<div class="card shadow-sm">
<img class="img-fluid" alt="Responsive image" src="{{ product.image.url }}">
<div class="card-body">
<p class="card-text">
<a class="text-dark text-decoration-none" href="{{ product.get_absolute_url }}">{{ product.title }}</a>
</p>
<div class="d-flex justify-content-between align-items-center">
<small class="text-muted">5 min read</small>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
</main>
{% endblock %}
Every time i enter '127.0.0.1:8000/search/ring' in url bar ,it raises error for category search mentioned above but it is working fine for product details i.e '127.0.0.1:8000/item/ring'.
you should probably change
<slug:slug>and<slug:category_slug>to<str:slug>and<str:category_slug>in your urls.py