Home > Software design >  Django url dispetcher route by id
Django url dispetcher route by id

Time:02-19

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 ?

CodePudding user response:

Your problem comes from your url settings your should add primary key to the url. Change this

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

To this

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

CodePudding user response:

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
  • Related