我查阅了大量的django AJAX表单教程,但每个教程都告诉您一种方法,没有一个是简单的,我有点困惑,因为我从来没有使用过AJAX。
我有一个名为" note“的模型,一个用于它的模型形式,在这个模板中,我需要每当note元素发送stop()信号(来自jQuery可排序) 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发送的数据并更新note对象?
https://stackoverflow.com/questions/7335780
复制相似问题