$(function () {
var loadForm = function () {
var btn = $(this);
$.ajax({
url: btn.attr("data-url"),
type: 'get',
dataType: 'json',
beforeSend: function () {
$("#modal").modal("show");
},
success: function (data) {
$("#modal .modal-content").html(data.html_task_form);
}
});
};
var saveForm = function () {
var form = $(this);
$.ajax({
url: form.attr("action"),
data: form.serialize(),
type: form.attr("method"),
dataType: 'json',
success: function (data) {
if (data.form_is_valid) {
$("#task-list").html(data.html_task);
$("#modal").modal("hide");
}
else {
$("#modal .modal-content").html(data.html_task_form);
}
}
});
return false;
};
// Create Task
$("#task-add-button").click(loadForm);
$("#modal").on("submit", ".js-task-add-form", saveForm);
// Update Task
$("#task-list").on("click", "#js-edit-task-button", loadForm);
$("#modal").on("submit", ".js-task-edit-form", saveForm);
});
data: {csrfmiddlewaretoken: getCookie('csrftoken')}
getCookie()
описана на той странице документации, что ты нашёл. <form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
{% endfor %}
<button type="submit"Submit</button>
</form>
var saveForm = function () {
var form = $(this);
var formData = new FormData(this);
$.ajax({
url: form.attr("action"),
data: formData,
contentType: false,
processData: false,
type: form.attr("method"),
success: function (data) {
if (data.form_is_valid) {
$("#task-list").html(data.html_task);
$("#modal").modal("hide");
}
else {
$("#modal .modal-content").html(data.html_task_form);
}
}
});
return false;
};
function ajax_post(url, form_id, block_id) {
var form = $(form_id).serialize();
$.ajax({
url: url,
type: 'POST',
data: form,
dataType: 'html',
success: function (data) {
$(block_id).html(data);
}
});
return false;
}
<form action="javascript:void(null);" onsubmit="ajax_post('{% url 'companies' project %}', '#project_company_form',
'#content')" id="project_company_form">
{% csrf_token %}
{{ form.company }}
....
<button type="submit"> Add </button>
</form>