我查阅了大量的Django Ajax表单教程,但每个教程都告诉您一种方法,没有一个是简单的,我有点困惑,因为我从未使用过Ajax.

I have a model called "note", a modelform for it, and inside the template I need that everytime a note element sends the stop() signal (from jQuery Sortables) django updates the object.

My current code:

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",
    });
}

The current code gets the data from the template and prints it in the terminal. I don't know how I can manipulate this data. I've seen some people manages the data through jqueryforms to send the data to django.

如何访问AJAX发送的数据并更新note对象?

推荐答案

Since you are using jQuery why not use the following:

<script language="JavaScript">
    $(document).ready(function() {
        $('#YOUR_FORM').submit(function() { // catch the form's submit event
            $.ajax({ // create an AJAX call...
                data: $(this).serialize(), // get the form data
                type: $(this).attr('method'), // GET or POST
                url: $(this).attr('action'), // the file to call
                success: function(response) { // on success..
                    $('#DIV_CONTAINING_FORM').html(response); // update the DIV 
                }
            });
            return false;
        });
    });
</script>

EDIT

正如 comments 中指出的那样,有时上述方法行不通.因此,请try 以下方法:

<script type="text/javascript">
    var frm = $('#FORM-ID');
    frm.submit(function () {
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                $("#SOME-DIV").html(data);
            },
            error: function(data) {
                $("#MESSAGE-DIV").html("Something went wrong!");
            }
        });
        return false;
    });
</script>

Jquery相关问答推荐

在添加_renderMenu方法时,是什么原因导致jQuery UI AutoComplete抛出TypeError?

如何用 jQuery / AJAX 替换表格的行

如何使用 jQuery 强制悬停状态?

加载外部 css 文件,如 jquery 中的脚本,这在 ie 中也兼容

jquery数据表隐藏列

Ajax 更新后在 jQuery 中重新绑定事件(更新面板)

当ID包含方括号时按ID查找DOM元素?

将参数附加到 URL 而不刷新

我可以限制 JavaScript 中数组的长度吗?

从附加元素获取 jQuery 对象的更简单方法

使用 JQuery 激活 bootstrap 选项卡

图片转Base64

jQuery .hasClass() 与 .is()

IE8 和 JQuery 的 trim()

如何使用 jQuery Validation Plugin 以编程方式判断表单是否有效

Jquery Select 所有具有 $jquery.data() 的元素

我应该在addClass之前使用hasClass吗?

将变量的值复制到另一个

jQuery 使用 AND 和 OR 运算符按属性 Select

jQuery中的wait()或sleep()函数?