-1

i am creating a comment update function using django and ajax, and i am trying to update the comment without refreshing the page, now when i click on the edit button, the exisiting comment get filled in the input box ready for edit, when i edit the text (comment) instead of saving the new edit to the old one; it goes ahead an create a new comment all together, how do i prevent that.

view.py


## edit comment
@csrf_exempt
def edit_comment(request):
    if request.method == "POST":
        id = request.POST.get("cid")
        comment = Comment.objects.get(pk=id)
        comment_data = {"id": comment.id, "comment": comment.comment, "video": comment.video.id}
        return JsonResponse(comment_data)

## Create comment
def ajaxComment(request):
    if request.method == "POST":
        pk = request.POST.get("id")
        video = Video.objects.get(id=pk)
        user = request.user
        comment = request.POST.get("comment")
        new_comment = Comment(user=user, video=video, comment=comment)
        new_comment.save()

        print(id)
        print(user)
        print(comment)
        print(video)

        response = "Comment Posted"
        return HttpResponse(response)


ajax code


// Edit
$('.comment-wrapper').on("click", ".btn-edit", function(){
    console.log("Edit Button Cliked");
    let id = $(this).attr("data-cid");

    console.log(id);

    mydata = {cid:id}

    $.ajax({
        url: "{% url 'edit-comment' %}",
        method:"POST",
        data:mydata,

        success: function(data){
            console.log(data);
            $("#id").val(data.id)
            $("#id").val(data.video)
            $("#comment").val(data.comment)
            // $(".new_comment").val(data.video)

            console.log(data.id);


        },
    })
})

$(document).on("submit", "#comment_form", function(e){
        e.preventDefault();
        let _comment = $("#comment").val()

        console.log("send button clicked")
        $.ajax({
            type: "POST",
            url: "{% url 'save-comment' %}",
            data:{
                id: $("#id").val(),
                comment: _comment,
                csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
            },
            success: function(){
                // Some code
            }

index.html

<button data-cid="{{ c.id }}" class="btn-edit">Edit</button>

what might be the best fix for this?

1 Answer 1

1

I Assume if it's an new or an edit you are posting to ajaxComment. If it's an edit just pass an extra POST with the PK of the comment:

def ajaxComment(request):
    if request.method == "POST":
        pk = request.POST.get("id")
        video = Video.objects.get(id=pk)
        user = request.user

        comment = request.POST.get("comment")

        if pk:
            # is an edit
            o = Comment.objects.get(pk=pk)
            o.comment = comment
            # you could chop these two out
            o.video = video
            o.user = user
            o.save()
            response = "Comment Edited"
        else:
            # is new
            new_comment = Comment(user=user, video=video, comment=comment)
            new_comment.save()
            response = "Comment Created"

        print(id)
        print(user)
        print(comment)
        print(video)

        return HttpResponse(response)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.