【发布时间】:2011-11-12 05:48:06
【问题描述】:
我查看了大量关于 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 发送的数据并更新 note 对象?
【问题讨论】:
标签: javascript jquery ajax django django-templates