def index(request):
# 查询各个分类的最新4条,最热4条数据
typelist = TypeInfo.objects.all()
# _set 连表操作
type0 = typelist[0].goodsinfo_set.order_by('-id')[0:4] # 图书的ID号,按照上传顺序自动排序
type01 = typelist[0].goodsinfo_set.order_by('-gclick')[0:4] # 按照点击量
type1 = typelist[1].goodsinfo_set.order_by('-id')[0:4]
type11 = typelist[1].goodsinfo_set.order_by('-gclick')[0:4]
type2 = typelist[2].goodsinfo_set.order_by('-id')[0:4]
type21 = typelist[2].goodsinfo_set.order_by('-gclick')[0:4]
type3 = typelist[3].goodsinfo_set.order_by('-id')[0:4]
type31 = typelist[3].goodsinfo_set.order_by('-gclick')[0:4]
type4 = typelist[4].goodsinfo_set.order_by('-id')[0:4]
type41 = typelist[4].goodsinfo_set.order_by('-gclick')[0:4]
type5 = typelist[5].goodsinfo_set.order_by('-id')[0:4]
type51 = typelist[5].goodsinfo_set.order_by('-gclick')[0:4]
increase=GoodsInfo.objects.all().order_by('-gclick')[5:10]
cart_num = 0
``
###### 运行结果及报错内容
File "D:\BaiduNetdiskDownload\csdn下载\fs_book\apps\df_goods\views.py", line 19, in index
type0 = typelist[0].goodsinfo_set.order_by('-id')[0:4] # 图书的ID号,按照上传顺序自动排序
File "C:\Users\北游而南归\AppData\Roaming\Python\Python37\site-packages\django\db\models\query.py", line
318, in __getitem__
return qs._result_cache[0]
IndexError: list index out of range
你的typelist
是空列表,获取元素之前判断一下,使用if len(typelist):
或if typelist: