Dynamic URL Routing Django

Question:

I Created a dynamic url routing/views for each one of the product on my website, Everything is working fine until I go to Cart/checkout and it loads on of the product page currently in Cart instead of Cart.html and Checkout.html

urlpatterns = {
    path('<str:pk>/', views.dynamic_product_view, name='productdetail'),
}

views.py:

def dynamic_product_view(request, pk=None):
    products = Product.objects.all()
    slug=None
    data = cartData(request)
    items = data['items']
    if pk is not None:
        try:
            slug = Product.objects.get(slug=pk)
        except:
            Http404()
    context = {
       'slug':slug,
       'products': products,
       'items': items
    }
    return render(request, 'product-details.html', context)

It’s currently working fine on any other Page like index, store and Products page but the problem appear in Cart and Checkout

Asked By: Hoshmand Qadir

||

Answers:

Replace

Http404()

To

return Http404()

‌‌‌‌‌

Answered By: M.J.GH.PY

Just adding as an answer instead of comment:

If you are working with dynamic URL routing that has no prefix it matches all the URLs that can appear on the site. Django evaluates the patterns in order they are defined so the dynamic one should be after all the specific ones and then when checkout and cart are not matched the dynamic one will process it.

In your case place the dynamic route after cart and checkout routes.

Answered By: preator