Show product page by category - send 2 var in url

You fetch products by category in this function

Urls

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

Views

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 http://127.0.0.1:8000/category/phone/redmi-8-pro

Template

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

Urls

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

Views

def catproslug(request, slug, proslug):
    d = Product.objects.get(slug=proslug)
    context = {
        'd': d,
        'category':category
    }
    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 views.py will be like

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