[Django]-Id(number) path in urls

8👍

✅

try this

urlpatterns = [
    path('topics/<topic_id>/', views.topic, name='topic'),
]

If you are expecting an integer, you could specify it as,

urlpatterns = [
    path('topics/<int:topic_id>/', views.topic, name='topic'),
]

Why topics/(?P<topic_id>\d+)/' is not working?

Actually you are mixing the usage.

If you are trying yo provide a regex included expression in your urls, use re_path().

So your pattern will be:

from django.urls import re_path

urlpatterns = [
    re_path('topics/(?P<topic_id>\d+)/', views.topic, name='topic'),
]

Leave a comment