我已经检查了很多有关Django AJAX表单的教程,但是每个教程都告诉您一种实现方式,它们都不是简单的,而且由于我从未使用过AJAX,所以我感到有些困惑。
我有一个名为“ note”的模型,它的模型形式,并且在模板内部,我需要每次note元素发送stop()信号(来自jQuery Sortables)时django更新对象。
我当前的代码:
views.py
def save_note(request, space_name):
"""
Saves the note content and position within the table.
"""
place = get_object_or_404(Space, url=space_name)
note_form = NoteForm(request.POST or None)
if request.method == "POST" and request.is_ajax:
msg = "The operation has been received correctly."
print request.POST
else:
msg = "GET petitions are not allowed for this view."
return HttpResponse(msg)
JavaScript:
function saveNote(noteObj) {
/*
saveNote(noteObj) - Saves the notes making an AJAX call to django. This
function is meant to be used with a Sortable 'stop' event.
Arguments: noteObj, note object.
*/
var noteID = noteObj.attr('id');
$.post("../save_note/", {
noteid: noteID,
phase: "Example phase",
parent: $('#' + noteID).parent('td').attr('id'),
title: $('#' + noteID + ' textarea').val(),
message: "Blablbla",
});
}
当前代码从模板获取数据并将其打印在终端中。我不知道该如何处理这些数据。我见过有人通过jqueryforms管理数据以将数据发送到django。
如何访问AJAX发送的数据并更新便笺对象?