简体   繁体   English

/delta/ 处的索引错误 - 列表索引超出范围 - Django

[英]IndexError at /delta/ - list index out of range - Django

The user selects two aircraft to compare from the list page.用户从列表页面中选择两架飞机进行比较。 However I get the following error: IndexError at /delta/ list index out of range .但是我收到以下错误: IndexError at /delta/ list index out of range

It's complaining about this line in particular:它特别抱怨这一行:

first_value = getattr(aircraft_to_compare[0], key) 

Is there an obvious error that I'm making here?我在这里犯了一个明显的错误吗?

View看法

def aircraft_delta(request):
  ids = [id for id in request.GET.get('ids') if id != ',']
  aircraft_to_compare = Aircraft.objects.filter(id__in=ids)

  property_keys = ['name', 'manufacturer', 'aircraft_type', 'body', 'engines',
                   'image', 'cost','maximum_range','passengers','maximum_altitude','cruising_speed',
                   'fuel_capacity','description','wing_span','length']

  column_descriptions = {
    'image': '',
    'name': 'Aircraft',
    'maximum_range': 'Range (NM)',
    'passengers': 'Passengers',
    'cruising_speed': 'Max Speed (kts)',
    'fuel_capacity': 'Fuel Capacity',
    'aircraft_type': 'Type',
    'body':'Body',
    'engines':'Engines',
    'cost':'Cost',
    'maximum_altitude':'Maximum Altitude',
    'description':'Description',
    'manufacturer':'Manufacturer',
    'wing_span':'Wing Span (FT)',
    'length':'Total Length (FT)'
  }

  data = []

  for key in property_keys:
    row = [column_descriptions[key]]


    first_value = getattr(aircraft_to_compare[0], key)
    second_value = getattr(aircraft_to_compare[1], key)

    if key not in ['image', 'name']:
        delta = abs(first_value - second_value)
    else:
        delta = ''

    row.append(first_value)
    row.append(delta)
    row.append(second_value)

    data.append(row)

  return render(request, 'aircraft/aircraft_delta.html', {
    'data': data
  })

IndexError at /delta/ list index out of range. /delta/ 列表索引超出范围的索引错误。 means that there is not data that was found by your model.意味着您的模型没有找到数据。 You might want to look into your db to see whether those Ids exist or not.您可能想查看您的数据库以查看这些Ids是否存在。 As per your code, there are no errors, so plz look at Aircraft.objects.filter(id__in=ids) a little more in deep.根据您的代码,没有错误,所以请更深入地查看Aircraft.objects.filter(id__in=ids)

Also its a good approach to use len(aircraft_to_compare) to check whether any data is present or not.这也是使用len(aircraft_to_compare)检查是否存在任何数据的好方法。

Hope this helps.希望这可以帮助。

You may not have found any records in the query您可能没有在查询中找到任何记录

aircraft_to_compare = Aircraft.objects.filter(id__in=ids)

Check the length of aircraft_to_compare or use try...except block while accessing items from that queryset.检查aircraft_to_compare的长度或在访问来自该查询集的项目时使用try...except块。

In Views, You are creating a list data=[] , then you are appending some values in that list.在视图中,您正在创建一个列表data=[] ,然后在该列表中附加一些值。 So the problem is when the list is appended to its max length there is no space left to append more values "list out of range".所以问题是当列表附加到其最大长度时,没有剩余空间可以附加更多“列表超出范围”的值。 You can try to make the list empty after each operation(comparison) using data.clear() .您可以尝试在每次操作(比较)后使用data.clear() .将列表清空data.clear() .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM