Fetch API PUT method not updating database

In case it helps anyone else, I was missing @csrf_exempt above the post function. I did also realise that since I was only making a minor change to my resource it is better practice to use a PATCH method instead of PUT.

def post(request, post_id):

        post = Post.objects.get(pk=post_id)
    except Post.DoesNotExist:
        return JsonResponse({"error": "No post found."}, status=404)

    if request.method == "GET":
        return JsonResponse(post.serialize())

    elif request.method == "PATCH":
        data = json.loads(request.body)
        post.likes = data["likes"]
        return HttpResponse(status=204)
        return JsonResponse({
            "error": "GET or PATCH request required."
        }, status=400)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top