EDIT: Added a new solution since the first was only about a line the author Woody1193 forgot to mention but implemented it already.
NEW:
I have actually had a similar problem and solved it by creating a custom POST function in javascript.
Copied from https://docs.djangoproject.com/en/1.9/ref/csrf/#ajax you can get the cookie with this snippet of code:
// using jQuery
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie('csrftoken');
Having read a solution regarding hidden input forms I was led to go for an analogous approach for tackling the select problem: JavaScript post request like a form submit
Here comes the customized part. I basically used mixed 2 already existent solutions which worked for me.
It actually is the second solution in the link above:
// Post to the provided URL with the specified parameters.
function post(path, parameters) {
var form = $('<form></form>');
form.attr("method", "post");
form.attr("action", path);
var csrf_field.setAttribute("name", 'csrfmiddlewaretoken');
csrf_field.setAttribute("value", getCookie('csrftoken'));
form.appendChild(csrf_field);
$.each(parameters, function(key, value) {
var field = $('<input></input>');
field.attr("type", "hidden");
field.attr("name", key);
field.attr("value", value);
form.append(field);
});
// The form needs to be a part of the document in
// order for us to be able to submit it.
$(document.body).append(form);
form.submit();
}
OLD:
I solved it by using the csrf_token in the beginning of the form.
Your code would look like:
<form method="post">
{% crsf_token %}
<!-- all the good stuff -->
</form>
You can find a better formulated description on how to (hopefully) solve your problem here: https://docs.djangoproject.com/en/1.9/ref/csrf/