CSRF令牌丢失或无效。 ->第一种形式使其他CSRF令牌工作失败

时间:2019-03-26 15:24:20

标签: ajax django csrf

因此,我有一个包含多个按钮的网站。这些按钮在表单内部,并使用此{CSRF}。但是,第一个按钮将不起作用。

这是HTML的摘要。

<form  method="post" id="post-form">
                      {% csrf_token %}
                      <button type="submit" name="startVm" class="btn btn-default btn-block d-none d-md-block">StartVM</button>
                </form>
                <form  method="post" id="post-form">
                      {% csrf_token %}
                      <button type="submit" name="stopVm" class="btn btn-default btn-block d-none d-md-block">StopVM</button>
                </form>

这是我使用的Ajax函数。

$('#post-form').on('submit', function(e){
        e.preventDefault();
        console.log("form submitted!")  // sanity check
        post();
    });
    // AJAX for posting
    function post() {
        console.log("create post is working!") // sanity check
        $.ajax({
            url : '', // the endpoint
            type : 'post', // http method
            data : {},
            csrfmiddlewaretoken: '{{ csrf_token }}',
            contentType: 'application/x-www-form-urlencoded',
            processData: true,
            // handle a successful response
            success : function() {
               alert("Thank you for your comment!");
                console.log("success"); // another sanity check
            },

            // handle a non-successful response
            error : function(xhr,errmsg,err) {
                $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
                    " <a href='#' class='close'>&times;</a></div>"); // add the error to the dom
                console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
            }
        });
    };

所以我说过了。 StartVM按钮将不起作用,并返回403错误。(禁止(CSRF令牌丢失或不正确。):/) 但是第二个没有问题。

这是view.py中的代码

def post (self, request):
    if request.method == 'POST' and 'startVm' in request.POST:
        print("startVM button")
        return HttpResponse("{}",
        content_type='application/json', status=204)
    if request.method == 'POST' and 'stopVm' in request.POST:
        print("stopVM button");
        return HttpResponse("{}",
        content_type='application/json', status=204)
    return HttpResponse("{}",
    content_type='application/json')

我正在返回状态204,因为e.preventDefault()无法正常工作,如果我单击一个按钮,它将刷新整个站点。

1 个答案:

答案 0 :(得分:0)

首先,id应该是唯一的,但是您在两个单独的表单上有ORDER BY Product, Color, Sellout DESC

您可以改为使用id="post-form",然后将JS更改为使用class="post-form"

或者,对于您问题中的模板,您可以使用包含两个按钮的单个.post-form标签。

接下来,您需要在表单数据中添加CSRF令牌

<form>

或者,您可以遵循文档中的suggestion,并为ajax请求添加一个data : {'csrfmiddlewaretoken': '{{ csrf_token }}'}, 标头,然后就无需在发布数据中包含令牌。