Page not found (404) - No Product matches the given query

I'm working on a Django blog, and having implemented slug for detail page. I've stumbled upon an issue.

Page not found (404) Request Method: GET Request

URL: http://127.0.0.1:8000/susu/ Raised

by: myshop.views.product_detail

No Product matches the given query.

This is my myshop/urls.py file

from django.conf.urls import url
from . import views

app_name = 'myshop'
urlpatterns = [
    url(r'^$', views.product_list, name='product_list'),
    url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
    url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
]

This is my urls.py file

from django.contrib import admin
from django.conf.urls import url, include
from myshop import views as mv
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('myshop.urls', namespace='myshop')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This is my views.py file

from django.shortcuts import render, get_object_or_404
from .models import Category, Product

# Create your views here.

def product_list(request, category_slug=None):
    category = None
    categories = Category.objects.all()
    products = Product.objects.filter(available=True)
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        products = products.filter(category=category)
    return render(request, 'shop/product/list.html', {'category': category, 'categories': categories, 'products': products})

def product_detail(request, product_slug):
    product = get_object_or_404(Product, slug=product_slug, available=True)
    return render(request, 'shop/product/detail.html', {'product': product})

And this is my models.py file

from django.db import models
from django.urls import reverse

# Create your models here.

class Category(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True)

    def __str__(self):
        return self.name
    
    def get_absolute_url(self):
        return reverse('myshop:product_list_by_category', args=[self.slug])

class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products', on_delete=None)
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    stock = models.PositiveIntegerField()
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('myshop:product_detail', args=[self.slug])

I have try to re-order the urls entries in url patterns from

urlpatterns = [
    url(r'^$', views.product_list, name='product_list'),
    url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
    url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
]

to

urlpatterns = [
    url(r'^$', views.product_list, name='product_list'),
    url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),
    url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
]

and the result is i can load http://127.0.0.1:8000/susu/ page, (susu is Category) but i can't load http://127.0.0.1:8000/susu-vanilla/ page, (susu-vanilla is Product) and this is the error

Page not found (404) Request Method: GET Request

URL: http://127.0.0.1:8000/susu-vanilla/ Raised

by: myshop.views.product_list

No Category matches the given query.

Can someone help me? Sorry for my bad english, and thanks

Upvotes: 1

Views: 1492

Answers (1)

Alasdair
Alasdair

Reputation: 308939

You can't have these two URL patterns. Django will always match the top one.

url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),

The typical solution is to add a prefix to one (or both) of the regexes, for example:

url(r'^(?P<product_slug>[-\w]+)/$', views.product_detail, name='product_detail'),
url(r'^categories/(?P<category_slug>[-\w]+)/$', views.product_list, name='product_list_by_category'),

Upvotes: 2

Related Questions