Reputation: 13
I'm trying out Django for a webshop, I'm following the instructions of a book(Django 3 by example), but there seem to be something missing.
I have tested if return to home in def product_list seen in views.py, so I believe the problem is in the "if category_slug:" condition or it is not receiving the slug.
In main urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('shop.urls', namespace='shop')),
]
In app urls.py
urlpatterns = [
path('', views.product_list, name='product_list'),
path('<slug:category_slug>/', views.product_list, name='products_list_by_category'),
path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),
]
In views.py:
def product_list(request, category_slug=False):
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})
Upvotes: 0
Views: 484
Reputation: 301
Maybe this can help
path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
Upvotes: 0
Reputation: 5943
Try this out in url_patterns
url(r'^(?P<category_slug>[-\w]+)/$', views.product_list, name='products_list_by_category'),
Upvotes: 1