Home > front end >  Dynamic link in Django always calls the first url path
Dynamic link in Django always calls the first url path

Time:12-06

In urls.py within urlpatterns I have below two lines

urlspatterns = [
...
path('<slug:productSlug>', ProductView.as_view(), name = 'viewProduct'),
path('<slug:boxSlug>', BoxView.as_view(), name = 'BoxView'),
...
]

In my html template I have two links

<a href="{% url 'viewProduct' item.productSlug %}" class="btn btn-outline-primary" tabindex="-1" role="button" aria-disabled="true">product view</a>

<a href="{% url 'BoxView' item.boxSlug %}" class="btn btn-outline-primary" tabindex="-1" role="button" aria-disabled="true">Box View</a>

The problem is even though I specified BoxView in the {% url 'BoxView' ... %} it keeps calling the viewProduct path. If I reverse the order of the two paths in urlPatterns then, it keeps calling the 'BoxView'. What I don't understand is it keeps calling whatever it finds first in urlPatterns.

CodePudding user response:

You something to distingish between the view type. For example you could:

urlspatterns = [
...
path('product_view/<slug:productSlug>', ProductView.as_view(), name = 'viewProduct'),
path('box_view/<slug:boxSlug>', BoxView.as_view(), name = 'BoxView'),
...
]
  • Related