Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMeicki Jeldal
1,446 PointsDjango list index out of range error
Im getting an error: list index out of range after trying going to my teams page and trying loading all the teams from the database but i then get the above error cannot see why its happening
Error file
Environment:
Request Method: GET
Request URL: http://chaos.jeldal.art/teams/2/
Django Version: 1.11.6
Python Version: 3.6.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'staff']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template /home/melonendk/webapps/victory_genki_dk/victory/staff/templates/base.html, error at line 0
list index out of range 1 : {% load static %}
2 :
3 : <!DOCTYPE html>
4 :
5 : <html lang="en">
6 : <head>
7 : <meta charset="utf-8">
8 : <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
9 : <meta name="apple-mobile-web-app-capable" content="yes">
10 : <link href="{% static 'img/icons/apple-touch-icon.png' %}" rel="apple-touch-icon" />
Traceback:
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in _resolve_lookup
882. current = current[bit]
During handling of the above exception ('Team' object is not subscriptable), another exception occurred:
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/melonendk/webapps/victory_genki_dk/victory/staff/views.py" in team
217. return render(request, 'team.html', context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/loader.py" in render_to_string
68. return template.render(context, request)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render
207. return self._render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render_annotated
957. return self.render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/loader_tags.py" in render
177. return compiled_parent._render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render_annotated
957. return self.render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/loader_tags.py" in render
72. result = block.nodelist.render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render
990. bit = node.render_annotated(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render_annotated
957. return self.render(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in render
1040. output = self.filter_expression.resolve(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in resolve
708. obj = self.var.resolve(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in resolve
849. value = self._resolve_lookup(context)
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/template/base.py" in _resolve_lookup
890. current = getattr(current, bit)
File "/home/melonendk/webapps/victory_genki_dk/victory/staff/models.py" in teamleaders_listable
22. string = leaders[0].extendeduser.nickname
File "/home/melonendk/webapps/victory_genki_dk/lib/python3.6/Django-1.11.6-py3.6.egg/django/db/models/query.py" in __getitem__
289. return list(qs)[0]
Exception Type: IndexError at /teams/2/
Exception Value: list index out of range
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsIt difficult to debug without seeing the code. Looking at the stacktrace, the two lines of your code shown are:
Since only the second line explicitly uses indexing, could it be possible that the
leaders
list is empty making the[0]
index undefined?