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
- [Answer]-Add instance methods to User object that uses related models
- [Answer]-Access URL parameters in django 1.7 template without using context processors
- [Answer]-Creating a 'delete cart' button in django
Source:stackexchange.com