Show product page by category - send 2 var in url

You fetch products by category in this function


path('category/<str:slug>', views.categoryslug, name="categoryslug"),


def categoryslug(request, slug):
    category = Category.objects.get(slug=slug)
    context = {
        'category': category,
        'products': category.product_set.all(),
    #return render(request, 'pages/catpro.html', context)

But I want to open a product through this link


{% url 'catproslug' product.slug d.slug %}


path('category/<str:slug>/<str:proslug>', views.catproslug, name="catproslug")


def catproslug(request, slug, proslug):
    d = Product.objects.get(slug=proslug)
    context = {
        'd': d,
    return render(request, 'pages/catpro.html', context)

so lets say you have a url which will browse all the categories

path('category/', views.categorylist, name="category-list")

also you have a url to browse all the products of the category

path('category/<str:product_category_slug>/', views.product_list, name="product-list")

And finally on click of the product the product detail url be like

path('category/<str:product_category_slug>/<str:product_slug>/', views.product_detail, name="product-detail")

And your will be like

def product_detail(request, product_category_slug=None, product_slug=None):
    # your logic to return product detail
Back to Top