Home > Back-end >  Page not found(404) : No category matches the given query
Page not found(404) : No category matches the given query

Time:01-16

Below is my error

Page not found(404)

No category matches the given query.

request method: GET
Request URL:    http://127.0.0.1:8000/store/slug/
Raised by:  store.views.product_in_category

Using the URLconf defined in rhizomeedu_prj.urls, Django tried these URL patterns, in this order:

admin/
noticeboard/
markdownx/
store/ [name='product_all']
store/ <slug:category_slug>/ [name='product_in_category']

The current path, store/slug/, matched the last one.

And this is the model of Category.

class Category(models.Model):
    name = models.CharField(max_length=200, db_index=True)
    meta_description = models.TextField(blank=True)

    slug = models.SlugField(max_length=200, db_index=True, unique=True, allow_unicode=True)

    class Meta:
        ordering = ['name']
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('store:product_in_category', args={"slug":self.slug})

Related part of views.py

def product_in_category(request, category_slug=None):
    current_category = None
    categories = Category.objects.all()
    products = Product.objects.filter(available_display=True)

    if category_slug:
        current_category = get_object_or_404(Category, slug=category_slug)
        products = products.filter(category=current_category)

    return render(request, 'store/product_list.html',
                  {'current_category': current_category, 'categories': categories, 'products': products})

And this is whole urls.py

 from django.urls import path, include
from .views import *

app_name = 'store'

urlpatterns = [
    path('', product_in_category, name="product_all"),
    path('<slug:category_slug>/', product_in_category, name="product_in_category"),
    path('<int:id>/<product_slug>/', ProductDetail.product_detail, name="product_detail"),
    path('create_product/', ProductCreate.as_view()),
    path('cart/', detail, name='detail'),
    path('add/<int:product_id>/', add, name='item_add'),
    path('remove/<int:product_id>/', remove, name='item_remove'),
    path('orders/create/', order_create, name='order_create'),
    path('orders/create_ajax/', OrderCreateAjaxView.as_view(), name='order_create_ajax'),
    path('orders/checkout/', OrderCheckoutAjaxView.as_view(), name='order_checkout'),
    path('orders/validation/', OrderImpAjaxView.as_view(), name='order_validation'),
    path('orders/complete/', order_complete, name='order_complete'),
    path('admin/order/<int:order_id>/', admin_order_detail, name='admin_order_detail'),
    path('admin/order/<int:order_id>/pdf/', admin_order_pdf, name='admin_order_pdf'),
    path('cart/', detail, name='detail'),
    path('cart/add/<int:product_id>/', add, name='product_add'),
    path('cart/remove/<int:product_id>/', remove, name='product_remove'),
]

Lastly, this is related part of the template product_list.html

<div >
            <ul >
                <li >
                    <a  href="/store/">전체</a>
                </li>
                {% for c in categories %}
                <li >
                    <a  href="{{c.get_absolute_url}}" style="color:black">{{c.name}}</a>
                </li>
                {% endfor %}
            </ul>
        </div>

Every time I click category other than 'All', it raises error mentioned before.

Also the url turns '127.0.0.1:8000/store/slug' which is bizzare because there's no category named 'slug'. What's wrong with my code? Help me.

CodePudding user response:

I guess this is wrong:

def get_absolute_url(self):
    return reverse('store:product_in_category', args={"slug":self.slug})

and needs to be changed to:

def get_absolute_url(self):
    return reverse('store:product_in_category', args={"category_slug":self.slug})

CodePudding user response:

args used instead of kwargs and wrong parameter name.

use this

return reverse('store:product_in_category', kwargs={"category_slug": self.slug})

CodePudding user response:

Check this by changing get_absolute_url,

def get_absolute_url(self):
        return reverse("store:product_in_category", args=[str(self.slug)])

and the reason for 404 page not found error might be because there's no category object with category_slug you are passing and it shows error in this line,

current_category = get_object_or_404(Category, slug=category_slug)
  • Related