user17388930
user17388930

Reputation:

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 ?

Upvotes: 1

Views: 600

Answers (2)

Godda
Godda

Reputation: 1001

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'),

Upvotes: 1

HermanTheGerman
HermanTheGerman

Reputation: 273

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

Upvotes: 0

Related Questions