简体   繁体   English

获取TypeError:发送ajax发布请求Django视图时'NoneType'对象不可迭代

[英]Getting TypeError: 'NoneType' object is not iterable when sending ajax post request django views

I want to print the list of all my checked choices in my django view function,but when i send throught ajax selected data,i get the following error in my console: 我想在django视图功能中打印所有选中选项的列表,但是当我发送ajax所选数据时,在控制台中出现以下错误:

Traceback (most recent call last):
  File "C:\Users\lislis\DJANGO~2\DJANGO~1.2\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Users\lislis\DJANGO~2\DJANGO~1.2\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\lislis\DJANGO~2\DJANGO~1.2\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\lislis\Django-VEnvs-Projects\django 1.11.2\src\rapport_sante\papa\views.py", line 78, in stat_ajax
    for d in tb_ids:
TypeError: 'NoneType' object is not iterable
[12/Aug/2017 11:53:07] "POST /sante/stat-ajax/ HTTP/1.1" 500 19573

Here is a part of my ajax post request: 这是我的ajax发布请求的一部分:

  var data =[];
            $(":checkbox:checked").each(function() {
                data.push($(this).val());
            });

        if(data.length==0)
        {
            alert("Veuillez cocher au moins une dps");
            alert(data.length);
        }
        else
        {

              $.ajax({
        url: "{% url 'papa:stat-ajax' %}",
        type:"post",
        data: {dpsID:data,csrfmiddlewaretoken: '{{ csrf_token }}'},
        success:function(data) 
        {
            //Some code
        }
      });

Here is my view function,where i would like to display all the data contained in the dpsID of my ajax POST request: 这是我的视图函数,我想在其中显示ajax POST请求的dpsID中包含的所有数据:

def stat_ajax(request):
    tb_ids=request.POST.get('dpsID')
    for d in tb_ids:
        print(d)

You are trying to access the data as Form encoded data, which you are passing a JSON in the body 您正在尝试以表单编码数据的形式访问数据,并在主体中传递了JSON

def stat_ajax(request):
    import json
    data = json.loads(request.body)
    tb_ids = data['dpsID']
    for d in tb_ids:
        print(d)

Here is what you should do: 1)First,befor sending your ajax request,should convert your data array into string: 这是您应该执行的操作:1)首先,在发送ajax请求之前,应将data数组转换为字符串:

 $.ajax({
        url: "{% url 'papa:stat-ajax' %}",
        type:"post",
        data: {dpsID:data.toString(),csrfmiddlewaretoken: '{{ csrf_token }}'},

2)In your view funtion,you should remove commas,from the POST['dpsID'] varibales because when you call the toString() method,commas are still there,or you dont want to extract a id which a value as comma.So you can do this: 2)在您的视图功能中,应从POST['dpsID']变量中删除逗号,因为调用toString()方法时,逗号仍然存在,或者您不希望提取一个值作为逗号的ID。因此,您可以执行以下操作:

def stat_ajax(request):
    #We create a list which will contain our value without a comma
    tb=[]

    #Here you remove commas from you POST['dpsID'] variable
    for i in request.POST['dpsID']:
        #if a comma is found,we dont do anything
        if i==",":
            pass

        #But if it's a number,we append it in our list
        else:
            tb.append(i)
    for d in tb:
        print(d)

看到!!

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

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