简体   繁体   中英

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:

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:

  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:

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

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:

 $.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:

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)

看到!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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