veryacademy / django-ecommerce-project

The Django-Ecommerce is an open-source project initiative and tutorial series built with Python and the Django Framework.
MIT License
399 stars 278 forks source link

No Product matches the given query. #8

Closed DaveJ61 closed 2 years ago

DaveJ61 commented 2 years ago

I feel embarassed to ask this question as I have solved this type of problem many times, but on this occasion I have failed. I was unable to determine the cause of this and despite copying the code from this repo, this error still persists. All url.py files and views.py are identical. I've been struggling for two days now and just cannot see the error. I'd be grateful for some assistance. Apologies but I really can't figure out how this issue tracker works and to me seems rather antiquated. I've wasted a lot of time trying to format this without any luck. Nothing works as expected. Anyway:-

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

Request Method: GET Request URL : http://localhost:8000/basket` Raised By : store.views.product_detail Using the URLconf defined in core.urls Django tried these URL patterns, in this order:core.urls Django tried these URL patterns, in this order:

1 - basket/
2 - [name='product_all']
3 - [name='product_detail']

The current path, basket, matched the last one.

This view is supposed to be raising the problem but I can't see why it would be...

def product_detail(request, slug): product = get_object_or_404(Product, slug=slug, in_stock=True) return render(request, 'store/products/single.html', {'product': product})

from django.urls import path

from . import views

app_name = 'store'

urlpatterns = [ path('', views.product_all, name='product_all'), path('slug:slug', views.product_detail, name='product_detail'), path('shop/slug:category_slug/', views.category_list, name='category_list'), ] `

1 - basket/
2 - [name='product_all']
3 - [name='product_detail']

The current path, basket, matched the last one.

This view is supposed to be raising the problem but I can't see why it would be...

`from django.shortcuts import get_object_or_404, render

from .models import Category, Product

def product_all(request): products = Product.products.all() return render(request, 'store/home.html', {'products': products})

def category_list(request, category_slug=None): category = get_object_or_404(Category, slug=category_slug) products = Product.objects.filter(category=category) return render(request, 'store/products/category.html', {'category': category, 'products': products})

def product_detail(request, slug): product = get_object_or_404(Product, slug=slug, in_stock=True) return render(request, 'store/products/single.html', {'product': product})'

My store/urls.py:

`from django.urls import path

from . import views

app_name = 'store'

urlpatterns = [ path('', views.product_all, name='product_all'), path('slug:slug', views.product_detail, name='product_detail'), path('shop/slug:category_slug/', views.category_list, name='category_list'), ]`

My core/urls.py:

`from django.conf import settings from django.conf.urls.static import static from django.contrib import admin from django.urls import include, path

urlpatterns = [ path('basket/', include('basket.urls', namespace='basket')), path('', include('store.urls', namespace='store')), path('admin/', admin.site.urls), ]

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

zander9648 commented 2 years ago

Hi Dave. Let's fix this. First question - does the repo work? If not I will fix that first. Else if not - please link to me a copy of your repo and I will download and take a look.

Please, please please keep badgering me if this process is delayed.

DaveJ61 commented 2 years ago

Hi Zander. Thanks for coming back to me so quickly, but I've actually managed to fix this. I had inadvertently placed the basket template directory in to the incorrect directory. Apologies but normally after a while I generally manage to fix things but in this case, I couldn't see the wood for the trees.