1

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

4
  • 1
    providing all relevant routes will likely help to pinpoint the issue Commented Sep 8, 2022 at 10:51
  • You should include all other views and url patterns. Commented Sep 8, 2022 at 11:08
  • put cart and checkout URL patterns before this generic one, otherwise it will 'catch' all requests. Commented Sep 8, 2022 at 13:39
  • @preator turns out this was the issue thank you and thanks everyone for helping Commented Sep 9, 2022 at 22:02

2 Answers 2

1

Replace

Http404()

To

return Http404()

‌‌‌‌‌

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.