我在迁移时遇到问题,当它最终运行时,它出现在主页上。其他一些页面也可以工作,它似乎只是主页。
错误消息:
Internal Server Error: /
Traceback (most recent call last):
File "C:\Users\pc\formula_zone\lib\site-
packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\pc\formula_zone\lib\site-
packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\pc\formula_zone\lib\site-
packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\pc\Downloads\formula_zone\mysite\racing\views.py", line 15,
in racing_stats
ra = races[0]
File "C:\Users\pc\formula_zone\lib\site-packages\django\db\models\query.py",
line 303, in __getitem__
return qs._result_cache[0]
IndexError: list index out of range
我认为它在views.py上,所以这也是代码。
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})
答案 0 :(得分:0)
尝试这样的事情:
ra = None
if race_id:
ra = Race.objects.get(rac_id=race_id)
elif races:
ra = races[0]
并且不要忘记在模板中处理None ra。