Django 获取用户历史浏览记录(基于Redis缓存)
1 from django_redis import get_redis_connection 2 3 4 class UserInfoView(LoginRequiredMixin, View): 5 '''用户中心-信息页''' 6 def get(self, request): 7 '''显示''' 8 # 获取用户的个人信息 9 user = request.user 10 address = Address.objects.get_default_address(user) 11 12 # 获取用户的历史浏览记录 13 con = get_redis_connection('default') 14 history_key = 'history_%d'%user.id 15 16 # 获取用户最新浏览的5个商品的id 17 sku_ids = con.lrange(history_key, 0, 4) # [2,3,1] 18 19 # 遍历获取用户浏览的商品信息 20 goods_li = [] 21 for id in sku_ids: 22 goods = GoodsSKU.objects.get(id=id) 23 goods_li.append(goods) 24 25 # 组织上下文 26 context = {'page':'user', 27 'address':address, 28 'goods_li':goods_li} 29 30 return render(request, 'user_center_info.html', context)