Django IndexError at / list index out of range
I was having problems migrating and when it finally worked this appeared on the home page. Some of the other pages work, and it seems to just be the home page.
Error Messages:
Internal Server Error: /
Traceback (most recent call last):
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserspcDownloadsformula_zonemysiteracingviews.py", line 15,
in racing_stats
ra = races[0]
File "C:Userspcformula_zonelibsite-packagesdjangodbmodelsquery.py",
line 303, in __getitem__
return qs._result_cache[0]
IndexError: list index out of range
I think its on the views.py so here's that code as well.
def racing_stats(request, race_id=None):
drivers = Driver.objects.all().order_by('lap_time')
races = Race.objects.all()
for racer in drivers:
driver_controller.update_race_time(racer)
if race_id:
ra = Race.objects.get(rac_id=race_id)
else:
ra = races[0]
buttons =
for r in races:
buttons.append((r.rac_id, r.race_name))
context =
'drivers': drivers,
'active_race': ra,
'buttons': buttons
return render(request, 'racing/racing_stats.html', context)
def race_list(request):
races = Race.objects.all()
context =
'races': races,
return render(request, 'racing/races.html', context)
def race_detail(request, pk):
race_ds = get_object_or_404(Race, pk=pk)
print(race_ds)
return render(request, 'racing/race_detail.html',
'race_detail': race_ds)
python django
add a comment |
I was having problems migrating and when it finally worked this appeared on the home page. Some of the other pages work, and it seems to just be the home page.
Error Messages:
Internal Server Error: /
Traceback (most recent call last):
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserspcDownloadsformula_zonemysiteracingviews.py", line 15,
in racing_stats
ra = races[0]
File "C:Userspcformula_zonelibsite-packagesdjangodbmodelsquery.py",
line 303, in __getitem__
return qs._result_cache[0]
IndexError: list index out of range
I think its on the views.py so here's that code as well.
def racing_stats(request, race_id=None):
drivers = Driver.objects.all().order_by('lap_time')
races = Race.objects.all()
for racer in drivers:
driver_controller.update_race_time(racer)
if race_id:
ra = Race.objects.get(rac_id=race_id)
else:
ra = races[0]
buttons =
for r in races:
buttons.append((r.rac_id, r.race_name))
context =
'drivers': drivers,
'active_race': ra,
'buttons': buttons
return render(request, 'racing/racing_stats.html', context)
def race_list(request):
races = Race.objects.all()
context =
'races': races,
return render(request, 'racing/races.html', context)
def race_detail(request, pk):
race_ds = get_object_or_404(Race, pk=pk)
print(race_ds)
return render(request, 'racing/race_detail.html',
'race_detail': race_ds)
python django
2
ra = races[0]is the problematic line. Looks likeRace.objects.all()returns an empty queryset.
– Dušan Maďar
Nov 15 '18 at 9:16
Thank you! I had to delete my database, so that may explain that problem. Going to add some items in there and then check
– Max Loyd
Nov 15 '18 at 9:18
add a comment |
I was having problems migrating and when it finally worked this appeared on the home page. Some of the other pages work, and it seems to just be the home page.
Error Messages:
Internal Server Error: /
Traceback (most recent call last):
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserspcDownloadsformula_zonemysiteracingviews.py", line 15,
in racing_stats
ra = races[0]
File "C:Userspcformula_zonelibsite-packagesdjangodbmodelsquery.py",
line 303, in __getitem__
return qs._result_cache[0]
IndexError: list index out of range
I think its on the views.py so here's that code as well.
def racing_stats(request, race_id=None):
drivers = Driver.objects.all().order_by('lap_time')
races = Race.objects.all()
for racer in drivers:
driver_controller.update_race_time(racer)
if race_id:
ra = Race.objects.get(rac_id=race_id)
else:
ra = races[0]
buttons =
for r in races:
buttons.append((r.rac_id, r.race_name))
context =
'drivers': drivers,
'active_race': ra,
'buttons': buttons
return render(request, 'racing/racing_stats.html', context)
def race_list(request):
races = Race.objects.all()
context =
'races': races,
return render(request, 'racing/races.html', context)
def race_detail(request, pk):
race_ds = get_object_or_404(Race, pk=pk)
print(race_ds)
return render(request, 'racing/race_detail.html',
'race_detail': race_ds)
python django
I was having problems migrating and when it finally worked this appeared on the home page. Some of the other pages work, and it seems to just be the home page.
Error Messages:
Internal Server Error: /
Traceback (most recent call last):
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:Userspcformula_zonelibsite-
packagesdjangocorehandlersbase.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserspcDownloadsformula_zonemysiteracingviews.py", line 15,
in racing_stats
ra = races[0]
File "C:Userspcformula_zonelibsite-packagesdjangodbmodelsquery.py",
line 303, in __getitem__
return qs._result_cache[0]
IndexError: list index out of range
I think its on the views.py so here's that code as well.
def racing_stats(request, race_id=None):
drivers = Driver.objects.all().order_by('lap_time')
races = Race.objects.all()
for racer in drivers:
driver_controller.update_race_time(racer)
if race_id:
ra = Race.objects.get(rac_id=race_id)
else:
ra = races[0]
buttons =
for r in races:
buttons.append((r.rac_id, r.race_name))
context =
'drivers': drivers,
'active_race': ra,
'buttons': buttons
return render(request, 'racing/racing_stats.html', context)
def race_list(request):
races = Race.objects.all()
context =
'races': races,
return render(request, 'racing/races.html', context)
def race_detail(request, pk):
race_ds = get_object_or_404(Race, pk=pk)
print(race_ds)
return render(request, 'racing/race_detail.html',
'race_detail': race_ds)
python django
python django
asked Nov 15 '18 at 9:14
Max LoydMax Loyd
559
559
2
ra = races[0]is the problematic line. Looks likeRace.objects.all()returns an empty queryset.
– Dušan Maďar
Nov 15 '18 at 9:16
Thank you! I had to delete my database, so that may explain that problem. Going to add some items in there and then check
– Max Loyd
Nov 15 '18 at 9:18
add a comment |
2
ra = races[0]is the problematic line. Looks likeRace.objects.all()returns an empty queryset.
– Dušan Maďar
Nov 15 '18 at 9:16
Thank you! I had to delete my database, so that may explain that problem. Going to add some items in there and then check
– Max Loyd
Nov 15 '18 at 9:18
2
2
ra = races[0] is the problematic line. Looks like Race.objects.all() returns an empty queryset.– Dušan Maďar
Nov 15 '18 at 9:16
ra = races[0] is the problematic line. Looks like Race.objects.all() returns an empty queryset.– Dušan Maďar
Nov 15 '18 at 9:16
Thank you! I had to delete my database, so that may explain that problem. Going to add some items in there and then check
– Max Loyd
Nov 15 '18 at 9:18
Thank you! I had to delete my database, so that may explain that problem. Going to add some items in there and then check
– Max Loyd
Nov 15 '18 at 9:18
add a comment |
1 Answer
1
active
oldest
votes
Try something like this:
ra = None
if race_id:
ra = Race.objects.get(rac_id=race_id)
elif races:
ra = races[0]
and don't forget to handle None ra in your template.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315949%2fdjango-indexerror-at-list-index-out-of-range%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try something like this:
ra = None
if race_id:
ra = Race.objects.get(rac_id=race_id)
elif races:
ra = races[0]
and don't forget to handle None ra in your template.
add a comment |
Try something like this:
ra = None
if race_id:
ra = Race.objects.get(rac_id=race_id)
elif races:
ra = races[0]
and don't forget to handle None ra in your template.
add a comment |
Try something like this:
ra = None
if race_id:
ra = Race.objects.get(rac_id=race_id)
elif races:
ra = races[0]
and don't forget to handle None ra in your template.
Try something like this:
ra = None
if race_id:
ra = Race.objects.get(rac_id=race_id)
elif races:
ra = races[0]
and don't forget to handle None ra in your template.
answered Nov 15 '18 at 10:24
Mohammad UmairMohammad Umair
2,1831721
2,1831721
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315949%2fdjango-indexerror-at-list-index-out-of-range%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
ra = races[0]is the problematic line. Looks likeRace.objects.all()returns an empty queryset.– Dušan Maďar
Nov 15 '18 at 9:16
Thank you! I had to delete my database, so that may explain that problem. Going to add some items in there and then check
– Max Loyd
Nov 15 '18 at 9:18