Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index Error: list index out of range in Django

Tags:

python

django

I'm using Django 1.7 with Django Rest Framework. I've written an API that detects the login of a user as follows:

def login_check(request):
    user = request.user
    if user.is_anonymous():
        return HttpResponse(json.dumps({
            'success': False
        }))
    else:
        try:
            user_obj = UserProfile.objects.get(user__pk=user.id)
        except UserProfile.DoesNotExist:
            main_obj = User.objects.get(pk=user.id)
            user_obj = UserProfile(user=main_obj)
            user_obj.save()
        fb_uid = SocialAccount.objects.filter(user_id=user.id, provider='facebook')
        print fb_uid[0].uid
        user_obj.profile_photo_url = "http://graph.facebook.com/{}/picture?width=300&height=300".format(fb_uid[0].uid)
        user_obj.save()
        serialized = UserProfileSerializer(user_obj)
        return Response(serialized.data, status=status.HTTP_200_OK)

I face an error with this view which shows the following traceback

IndexError at /loginCheck/
list index out of range
Request Method: GET
Request URL:    http://localhost:8000/loginCheck/
Django Version: 1.7.4
Exception Type: IndexError
Exception Value:    
list index out of range
Exception Location: f:\App\venv\lib\site-packages\django\db\models\query.py in __getitem__, line 178
Python Executable:  f:\App\venv\Scripts\python.exe
Python Version: 2.7.6
Python Path:    
['f:\\App',
 'f:\\App\\venv\\lib\\site-packages\\psycopg2-2.6-py2.7-win32.egg',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'f:\\App\\venv\\DLLs',
 'f:\\App\\venv\\lib',
 'f:\\App\\venv\\lib\\plat-win',
 'f:\\App\\venv\\lib\\lib-tk',
 'f:\\App\\venv\\Scripts',
 'c:\\Python27\\Lib',
 'c:\\Python27\\DLLs',
 'c:\\Python27\\Lib\\lib-tk',
 'f:\\App\\venv',
 'f:\\App\\venv\\lib\\site-packages']

I'm not entirely sure if this is an error in my code or Django's query.py. I'd appreciate help in figuring out the problem here

like image 378
Newtt Avatar asked May 04 '15 11:05

Newtt


People also ask

How does Django handle list index out of range?

“List index out of range” error occurs in Python when we try to access an undefined element from the list. The only way to avoid this error is to mention the indexes of list elements properly.

How do I fix list index out of range?

The Python IndexError: list index out of range can be fixed by making sure any elements accessed in a list are within the index range of the list. This can be done by using the range() function along with the len() function.

How do you stop index out of range error in Python?

Solution using range() and len(): Subsequently preventing you from receiving the 'list index out of range' error in python. The range() function returns a sequence of numbers starting from 0, and ends at the integer passed as a parameter. We use this method along with the len() function.

What does it mean when a list index is out of range?

Generally, list index out of range means means that you are providing an index for which a list element does not exist.


1 Answers

Try to use the first() method instead of [0] indexing of queryset:

so_account = SocialAccount.objects.filter(user_id=user.id,
                                          provider='facebook').first()
if so_account:
    fb_uid = so_account.uid
    ...
like image 116
catavaran Avatar answered Sep 28 '22 07:09

catavaran