SORU
7 EYLÜL 2011, ÇARŞAMBA


Nasıl AJAX ile django form POST & jQuery

Ben zaten teslim ton eğitimler için django AJAX formları, ama her biri size bir şekilde yapıyor, hiçbiri basit ve ben biraz şaşkın zamandan beri hiç çalışmamış olan AJAX.

Ben bir model olarak "not", bir modelform için, ve içinde şablona ihtiyacım olan her bir element not gönderir stop() sinyali (jQuery Sortables) django güncellemeleri nesne.

Benim şu anki kod:

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

Geçerli kodu şablondan verileri alır ve terminale basar. Bu verileri işlemek nasıl yapacağımı bilmiyorum. Bazı insanlar veri django göndermek için jqueryforms üzerinden veri yönetir gördüm.

Nasıl verileri AJAX tarafından gönderilen erişim ve not nesne güncelleyebilir miyim?

CEVAP
7 EYLÜL 2011, ÇARŞAMBA


JQuery kullanarak neden aşağıdakileri kullanın: olduğundan

<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>

EDİT

Açıklamalarda belirttiği gibi bazen yukarıdaki işe yaramaz. Bu yüzden aşağıdakileri deneyin:

<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>

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • B4ROK

    B4ROK

    1 EKİM 2008
  • CaptainDisillusion

    CaptainDisil

    18 EYLÜL 2007
  • Michael Lummio

    Michael Lumm

    25 Mayıs 2007