Home > Enterprise >  Django Rest Framework"detail": "Method \"GET\" not allowed."
Django Rest Framework"detail": "Method \"GET\" not allowed."

Time:07-02

I;m trying to build api by using django rest framework but I got the issue.

@api_view(['PUT', ])
def api_update_blog_view(request, title):
    try:
        post = Post.objects.get(title=title)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'PUT':
        serializer = PostSerializer(post, data=request.data)
        data = {}

        if serializer.is_valid():
            serializer.save()
            data["success"] = "update successful"
            return Response (data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
urlpatterns = [

    path('<str:title>/update', api_update_blog_view, name="update"),


]

When I'm trying to update post I can see "detail": "Method "GET" not allowed." error. Where is the issue ?

CodePudding user response:

It happens when you use the wrong method to call the api. For example you defined the route method as PUT in your view but accessing it by GET method, hence the error. As of this, you would need a tool to test your apis with proper methods. Try using Postman or Thunder Client for VS code.

  • Related