PacktPublishing / Django-3-by-Example

Django 3 by Example (3rd Edition) published by Packt
https://djangobyexample.com/
MIT License
715 stars 690 forks source link

Chapter 7: Product Detail page does not dynamically link product data (stays static) #95

Closed lpandey closed 3 years ago

lpandey commented 3 years ago

Hi,

I am following along the code on the Chapter. Everything else before the product detail page worked as expected but the product detail page failed to load any data associated with any product (see image) on clicking any product on the product listing page. The click redirects to the product details page, however the appropriate data doesn't load. Any help would be appreciated.

Screen Shot 2021-08-12 at 5 35 35 PM

The detail.html file inside "/templates/shop/product" is exactly the same as the code on page [236-237] of the text and the appropriate file on GitHub. Below is the code for reference...

Screen Shot 2021-08-12 at 5 46 53 PM

The views.py code is: `from django.shortcuts import render, get_object_or_404 from .models import Category, Product

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, id, slug): product = get_object_or_404(Product, id=id, slug=slug, available=True)

return render(request,
              'shop/product/detail.html',
              {'product': Product})`

and the urls.py code in the app folder is: `from django.urls import path from . import views

app_name = 'shop'

urlpatterns = [ path('', views.product_list, name='product_list'), path('/', views.product_list, name='product_list_by_category'), path('//', views.product_detail, name='product_detail'), ]`

lpandey commented 3 years ago

Realized the error that was causing it -- the product object that was passed onto get_object_or_404 on the views.py should have been 'product' instead of 'Product'.