Yair V.
Yair V.

Reputation: 119

Django urlpatterns regex is imprecise, getting 404?

I'm working on Django API URLs, and trying to recognize this type of HTTP request:
DELETE http://localhost:8000/api/unassigned_events/dddd-dd-dd/d or dd/ - d for digit, whilst saving each sector in an argument.
e.g. DELETE http://localhost:8000/api/unassigned_events/2019-06-20/1/

My regex path expression is:
path(r'^api/unassigned_events/(?P<date>[0-9]{4}-[0-9]{2}-[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$', UnassignedClassRequests.as_view(), name='delete')

The HTTP request is the given example above, but I'm receiving a 404 error instead of the view's functionality.

Here's the to-be-called view method:

class UnassignedClassRequests(APIView):  

@staticmethod                                                                         
def delete(request):                                                                                                
UnassignedEvents.objects.filter(date=request.date, cls_id=request.cls_id).delete()
return HttpResponse(status=status.HTTP_201_CREATED)  

and the error I'm getting on Chrome:
DELETE http://localhost:8000/api/unassigned_events/2019-06-20/1/ 404 (Not Found).

I've also tried this regex expression for the path, not succeeding:

path(r'^api/unassigned_events/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$' UnassignedClassRequests.as_view(), name='delete')

What am I doing wrong?

Upvotes: 1

Views: 134

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477210

Django's path(..) [Django-doc] does not use regular expression syntax. You can use re_path(..) [Django-doc] for that:

from django.urls import re_path

urlpatterns = [
    re_path(r'^api/unassigned_events/(?P<date>[0-9]{4}-[0-9]{2}-[0-9]{2})/(?P<cls_id>[0-9]{1,2})/$', UnassignedClassRequests.as_view(), name='delete'),
    # ...
]

Upvotes: 2

Related Questions