I have a django application and I have 2 url paths that are only different in the last part which is the path convertors:
path('questions/<pk>', views.QuestionDetailView.as_view(), name='question_detail'),
path('questions/<slug:tag_slug>', views.QuestionListView.as_view(), name='questions_by_tag')
When I go to 127.0.0.1:8000/questions/1 it's ok and it shows the correct results, but when I go to 127.0.0.1:8000/questions/something(something is a slug) it says page not found!(It must use the seconed url path but it does not!)
When I change the paths order it shows the second one correctly and the other one is a problem! Can anybody help me please?
CodePudding user response:
You did not specify a path converter for <pk>
, hence it will use the <str:…>
path converter [Django-doc], and <str:…>
is a superset of <slug:…>
, hence everything that is matched by <slug:…>
is matched by <str:…>
as well, and thus the second pattern will never fire.
You should use the <int:…>
path converter for integers, so:
path('questions/<int:pk>/', views.QuestionDetailView.as_view(), name='question_detail'),
path('questions/<slug:tag_slug>/', views.QuestionListView.as_view(), name='questions_by_tag')