regular expression not recognized by django

48 Views Asked by At

I'm new to Django and I'm trying to create a simple path to later link it to the database, for now: I created this URL path in my music app:

urlpatterns = [
# /music/
path('', views.index, name='index'),

# /music/*****/
path(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),
]

And I added this to the views.py:

def detail(request, album_id):
    return HttpResponse("<h2>Details for Album id: " + str(album_id) + "</h2>")

But it doesn't work, every time I ask for this url: http://localhost:8000/music/2 I got "Page not found"

Please help. Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

We can use django path converts for your use case. So, please change

path(r'^(?P<album_id>[0-9]+)/$', views.detail, name='detail'),

to

path('<int:album_id>/', views.detail, name='detail'),

Reference: https://learnbatta.com/blog/custom-path-converters-in-django-17/