Search code examples
pythondjangononetype

object of type 'NoneType' has no len() :


def send_ivr_calls(sp_orders, base_url, api_key, extra_data):
    for contact in sp_orders:
        if len(contact) == 10:
            contact = '0'+contact

File "views.py", line 43, in calls if len(contact) == 10:
TypeError: object of type 'NoneType' has no len()

How can I check whether the sp_orders list does not contain any Nones?


Solution

  • Try this:

    def send_ivr_calls(sp_orders, base_url, api_key, extra_data):
        for contact in sp_orders:
            if contact and len(contact) == 10:
                    contact = '0'+contact
    

    This is ensures contact is not None before you try to get its length. Credit to @Moses Koledoye for pointing out short-circuiting.