在做商城项目中浏览足迹时,我利用浏览商品的ID和浏览的时间保存到browse表中,然后在我的足迹页面根据最近日期进行展示
条件:每天/个商品只能如一次表
后台代码如下:
#存储浏览足迹到browse表 def good_browse(request): id= request.POST.get('id') now_time = time.strftime('%Y-%m-%d') ret = Browse.objects.all().values('browse_time') rr = [str(i['browse_time']).split(' ')[0] for i in list(ret)] res = Browse.objects.filter(good_id=int(id)) #进行每天/个商品只能如一次表判断 if now_time in rr and res: return HttpResponse("fail") else: r = Browse(good_id=int(id)) r.save() return HttpResponse("ok") #展示浏览足迹 class Browse_show(APIView): def post(self,request): res = Browse.objects.all().values('browse_time','good_id').order_by('-browse_time') rr = [str(i['browse_time']).split(' ')[0] for i in list(res)] rr = set(rr) rr = sorted(list(rr),reverse=True) res=list(res) ret = [] browse_time=[] for i in res: ret.append(i['good_id']) browse_time.append(str(i['browse_time'])) rss = Good.objects.filter(id__in=ret) me = serialize('json',rss,ensure_ascii=False) return Response([me,rr,browse_time])