【问题标题】:I want to know why an error occurred when I sent an array to view with jquery ajax (django,jquery)我想知道为什么当我发送一个数组以使用 jquery ajax (django,jquery) 查看时发生错误
【发布时间】:2019-10-18 00:20:19
【问题描述】:

你好,我有一个问题

我正在尝试使用 jquery ajax 实现行删除。

我发送了一个数组以在 jquery ajax 中查看

此时出现错误。报错内容如下。

错误信息:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\django_inflearn2\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\django_inflearn2\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\django_inflearn2\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\django_inflearn2\todo\views.py", line 23, in todo_delete_ajax
    todo_ids = request.POST['todo_arr']
  File "C:\django_inflearn2\venv\lib\site-packages\django\utils\datastructures.py", line 80, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'todo_arr'
[02/Jun/2019 06:11:29] "POST /todo/todo_delete_ajax/ HTTP/1.1" 500 19607

如果你知道是什么原因以及如何解决请告诉我

谢谢~!

jquery , ajax

$('#todo_delete_button').click(function(e){
    e.preventDefault();
    // todo_check
    alert("삭제 버튼 ")
    // Get checked checkboxes
    var todo_arr = [];
    $('.td_check').each(function() {
        if (jQuery(this).is(":checked")) {
            var id = this.id;
            todo_arr.push(id);
        }
    });
    alert('todo_arr : '+ todo_arr)

    $.ajax({
      type: "POST",
      url: 'todo_delete_ajax/',
      data: {
          todo_arr:todo_arr,
          csrfmiddlewaretoken: '{{ csrf_token }}'
      },
        success: function(result) {
            alert('todo_delete_ajax is success ');
        }
    });
})

网址格式

    path('todo_delete_ajax/',views.todo_delete_ajax, name ="todo_delete_ajax"),

查看


def todo_delete_ajax(request):
    # print("request " , request )
    todo_ids = request.POST['todo_arr']
    print("todo_ids : ", todo_ids)

    return redirect('/todo/')

【问题讨论】:

    标签: jquery arrays ajax django view


    【解决方案1】:

    我看到您只是试图将选中的复选框的 ID 发送到服务器。

    这意味着如果没有选中的复选框,那么 todo_arr 将变为 null

    你需要给这种情况一个机会:

    def todo_delete_ajax(request):
        todo_ids = request.POST.get("todo_arr", None)
    
        // check if there are any todos 
        if todo_ids:
            print("todo_ids : ", todo_ids)
            return redirect('/todo/')
    
        // else, do something else
    

    request.POST.get 确保如果 todo_arr 为空,则 todo_ids 为 None。你得到这个错误是因为你试图得到不存在的东西。

    【讨论】:

    • 你能告诉我为什么 todo_arr 打印为 None 谢谢你告诉我~!
    • request.POST 是一本字典。这就是为什么您可以这样做request.POST['todo_arr'],它只是获取键todo_arr 的值。但是,如果没有todo_arr 这样的键或者todo_arr 为空怎么办? request.POST.get 所做的是获取todo_arr 的值(如果它存在,如果它不存在null),或者简单地将其值设为None。这将使我们能够检查一个键是否有一些价值。在您的情况下,它类似于if todo_ids == None。如果没有选中复选框,它将打印None
    • 谢谢,但是这个问题通过以下方式解决了。例如)todo_ids = request.POST.getlist('todo_arr[]')
    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多