Search code examples
djangodjango-rest-frameworkdjango-viewsputhttp-method

PUT request not appearing in allowed requests using ModelViewSet


I'm not able make put request using ModelViewSet like in the documentation. My views, serializers are as below

class PostsViewSet(viewsets.ModelViewSet):
    queryset = PostsModel.objects.all()
    serializer_class = PostsSerializer

class PostsSerializer(serializers.ModelSerializer):
    class Meta:
        model=PostsModel
        fields=('id','title', 'author', 'body')

PUT method is there in allowed methods as you can see in the picture. enter image description here

And this is my posts.urls.py i.e., my app

router = DefaultRouter()
router.register('', PostsViewSet)


    urlpatterns = [
        path('', include(router.urls))
    ]

and this is my root urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/posts', include('posts.urls')),
]

and in response for

http://localhost:8000/api/posts/1/

Response


Solution

  • I think the problem is here:

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('api/posts', include('posts.urls')),  # <-- Here
    ]
    

    It should be:

    path('api/posts/', include('posts.urls')),   # need to append slash after posts
    

    Here, there is nothing wrong with PUT request, the problem is with routing itself. When you are hitting /posts/1 its not being found by django(because the configuration was not correct).