[Answer]-Where to get the value when sending the request?

1👍

Put it in your AJAX request.

    beforeSend: function(xhr, settings) {
        xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
    },

Sometimes it does not work in some browsers (in my case it is chrome), so you can add these to data you are sending to view:

data['csrfmiddlewaretoken'] = $.csrf_token; 

$.csrf_token is a global object for me, I put token printing it to template directly:

<script type="text/javascript">
    $.csrf_token = '{{ csrf_token }}';
</script>

0👍

Your problem is explained in the django docs.

Be aware, that if no form is rendered django might not send a csrf token, you’ll have to use the ensure_csrf decorator

👤wastl

Leave a comment