Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django similar url pattern issue

I am working on a products comparison module and have url patterns like below:

path('comparison/<slug:slug1>-vs-<slug:slug2>/', views.compare_two_products, name="compare_two_products"),
path('comparison/<slug:slug1>-vs-<slug:slug2>-vs-<slug:slug3>/', views.compare_three_products, name="compare_three_products"),

The issue is that Django (3.2.6) always matches the first pattern and returns 404 when I try to access the second pattern. However if I comment out the first pattern, then it matches the third pattern just fine. I want to get both the patterns working in the format slug-vs-slug-vs-slug. Any suggestions on what I might be doing wrong ?

Thanks in advance.

like image 528
DEV Avatar asked Mar 13 '26 11:03

DEV


1 Answers

Just change the order or URLs, like

path('comparison/<slug:slug1>-vs-<slug:slug2>-vs-<slug:slug3>/', views.compare_three_products, name="compare_three_products"),
path('comparison/<slug:slug1>-vs-<slug:slug2>/', views.compare_two_products, name="compare_two_products"),

that should work

like image 145
Sumithran Avatar answered Mar 16 '26 06:03

Sumithran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!