'Django url dispetcher route by id

this is my views.py

class UserAPI(generics.RetrieveAPIView):
    permission_classes = [permissions.IsAuthenticated,]
    serializer_class = UserSerializer

    def get_object(self):
        return self.request.user

and this is my url.py

path('V1/api/users/', UserAPI.as_view(), name='user'),

when i enter id,mail,username and go to

localhost/v1/api/users/1  want to open user's which id is 1.

what is best solutions ?



Solution 1:[1]

path('V1/api/users/<int:user_id>', UserAPI.as_view(), name='user'),


class UserApi(generics.RetrieveAPIView):

    permission_classes = [permissions.IsAuthenticated,]
    serializer_class = UserSerializer

    def get(self, request, user_id, format=None):

        print(user_id) # do whatever you want with the user id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HermanTheGerman