Home > database >  How to route URL pattern with URL path separator?
How to route URL pattern with URL path separator?

Time:09-14

I have to route the following URL pattern to view api/v1/{resourceId}/owners in DRF

But the problem is resourceID contains / in it. eg api/v1/somethings/value/owners the additional / causing get 404 resource not found exception

Is there way to route the URL and get resourceID in view

My urls.py

path('api/v1/<str:resource_id>/owners', ResourceOwnershipView.as_view())

views.py

class ResourceOwnershipView(APIView):

    def get(self, request: HttpRequest, resource_id: str) -> Response:
        # do something with resourceID

CodePudding user response:

<str:> probably doesn't allow slashes in it. I would try to use regular expression version of routing definition re_path:

https://docs.djangoproject.com/en/4.1/ref/urls/#re-path

  • Related