Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamic link in Django always calls the first url path

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'),
...
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading