简体   繁体   English

Django:更新创建新的 object 而不是更新现有的 django?

[英]Django: update creating new object instead of updating existing in django?

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;我正在使用 django 和 ajax 创建评论更新 function,并且我正在尝试在不刷新页面的情况下更新评论,现在当我点击编辑框以进行编辑时,我正在编辑之前文本(评论)而不是将新编辑保存到旧编辑; it goes ahead an create a new comment all together, how do i prevent that.它继续创建一个新的评论,我该如何防止它。

view.py视图.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 ajax代码


// 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索引.html

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

what might be the best fix for this?什么可能是最好的解决方法?

I Assume if it's an new or an edit you are posting to ajaxComment .我假设您发布到ajaxComment是新的还是编辑的。 If it's an edit just pass an extra POST with the PK of the comment:如果它是一个编辑,只需传递一个带有评论 PK 的额外 POST:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM