Reputation: 23
Stumped with this one, I need to be able to call an article via a pk or slug, for example user can do https://www.website.com/1 or https://www.website.com/article-slug both show the same article/page.
And if neither 1 or slug exist show default article or nothing found.
path('/', views.index, name='index’), path('slug:slug/', views.index, name='index’)
Not sure how to proceed.
Upvotes: 0
Views: 530
Reputation: 850
urlpatterns = [
path('<int:pk>/', views.index),
path('<slug:slug>/',views.index)
]
note
: remove the name attribute in the path because Django cannot decide where to go because the name is a unique identifier for each URL in your system or just make their names different.
another approach:
urlpatterns = [
path('<str:id_or_slug>/', views.index)
]
def get(self, request, id_or_slug):
try:
# get element by pk
try:
#get element by slug
except:
#return that the element is not found
Upvotes: 1