DEV
DEV

Reputation: 33

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.

Upvotes: 3

Views: 59

Answers (1)

Sumithran
Sumithran

Reputation: 6565

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

Upvotes: 1

Related Questions