简体   繁体   English

在Django会话中处理异步Ajax调用

[英]Handling async ajax calls within django session

I have a Django application, where the template contains for loop in javascript, which iterates all of the check-boxes in some table. 我有一个Django应用程序,该模板包含javascript中的for循环,该循环迭代某些表中的所有复选框。 For each check box, I send an ajax request to a view function, where I want to save the id of the check box in a list or remove the id from the list (depends on the check state). 对于每个复选框,我向视图函数发送一个ajax请求,我想在其中将复选框的ID保存在列表中或从列表中删除ID(取决于检查状态)。 I need the list to be a part of the request.session dictionary. 我需要该列表成为request.session词典的一部分。 The results show me that the ajax calls are asynchronous and that makes my list to be wrongly updated, and inconsistent. 结果表明,Ajax调用是异步的,这使我的列表被错误地更新且不一致。 Are there some thread safe data structures which I can store as part of the session, and ensure sync list updating? 我可以在会话中存储一些线程安全的数据结构,并确保同步列表更新吗?

JavaScript and Ajax: JavaScript和Ajax:

function checkAll(source, type) {
            checkboxes = document.getElementsByName(type);
            for(var i=0, n=checkboxes.length;i<n;i++) {
                if (checkboxes[i].checked != source.checked) {
                    checkboxes[i].checked = source.checked;
                    select_row(checkboxes[i], source.checked);
                }
            }
        }

function select_row(row_selector, is_checked) {
            is_box_checked = typeof is_checked !== 'undefined' ? is_checked : row_selector.checked;
            request = {
                url: "{% url 'set_check_box' %}",
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                data: {
                    csrfmiddlewaretoken: "{{ csrf_token }}",
                    checked: is_box_checked,
                    check_box_id: row_selector.id,
                    type: row_selector.name
                },
                error: function(response, status, error_msg) {
                    console.log(error_msg);
                }
            };
            $.ajax(request);
        }

View function: 查看功能:

def set_check_box(request):
    request.session.modified = True
    check_box_list = list(request.session['connects_check_boxes_id_list'])
    check_box_id = request.POST["check_box_id"]
    is_checked = json.loads(request.POST['checked'])
    if is_checked:
        check_box_list.append(check_box_id)
    else:
        check_box_list.remove(check_box_id)
    request.session['connects_check_boxes_id_list'] = list(check_box_list)
return HttpResponse("")

All I had to do is set async option to false as part of the request parameters. 我要做的就是将async选项设置为false作为请求参数的一部分。

function select_row(row_selector, is_checked) {
            is_box_checked = typeof is_checked !== 'undefined' ? is_checked : row_selector.checked;
            request = {
                url: "{% url 'set_check_box' %}",
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                async: false,
                data: {
                    csrfmiddlewaretoken: "{{ csrf_token }}",
                    checked: is_box_checked,
                    check_box_id: row_selector.id,
                    type: row_selector.name
                },
                error: function(response, status, error_msg) {
                    console.log(error_msg);
                }
            };
            $.ajax(request);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM