Ernst
Ernst

Reputation: 514

Expected view to be called with a URL keyword argument named "user_token"

I am using Django Rest Framework and here is my view:

class DeleteUserView(generics.DestroyAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = UserSerializer
    queryset = User.objects.all()
    lookup_field = 'user_token'

and my urls.py:

from django.urls import path
from .views import CreateUserView, DeleteUserView

urlpatterns = [
    path('add_user/', CreateUserView.as_view()),
    path('delete_user/', DeleteUserView.as_view()),
]

serializer.py

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('user_token',)

I am trying to delete user by specific token but it doesn't work...I am using Postman and provide user_token in Body

Upvotes: 1

Views: 875

Answers (1)

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

If you set lookup_field paremeter, it basically looks for that variable in your URLconfig. Eg.

path('delete_user/(?P<user_token>[-\w]+)/', DeleteUserView.as_view()),

If you specify the URL like above, and then call http://127.0.0.1:8000/delete_user/1/, it should work

Note

In Your case, if you are going to have CRUD views like add_user, delete_user, update_user, then I suggest you use a model viewset

Upvotes: 1

Related Questions