utils:
# 商品多级联动 def get_category(categorys)->dict: ''' :param:商品类性 :return: {{[],[]},{[],[]},{[],[]}} ''' dict1 = forCategory(categorys) return dict1 def forCategory(categorys): '''递归''' sum = {} # if str(categorys) == "<QuerySet []>": # return Goods.objects.filter(category=category) for category in categorys: new_category = Category.objects.filter(category_pid=category.id) dict1 = forCategory(new_category) if dict1 == {}: dict1 = Goods.objects.filter(category=category) sum[category] = dict1 return sum
'''个人虚拟购物车''' import datetime from app01.models import Cart class PersonCart(): '''个人虚拟购物车''' def __init__(self): self.cart = None self.time = datetime.datetime.now() # 创建浏览者购物车 def getCart(self,goodsId,cart_num,cartIdList:list)->list: self.cart = Cart(goods_id=goodsId,member_id=16,cart_num=cart_num) self.cart.save() cartIdList.append(self.cart.id) return cartIdList # 删除浏览者购物车 @staticmethod def delCart(cartIdList:list): '''''' for cartId in cartIdList: Cart.objects.filter(id=cartId).delete() # 浏览者购物添加到用户购物车里 @staticmethod def addCart(cartIdList:list,memberId): if cartIdList is not None: for cartId in cartIdList: Cart.objects.filter(id=cartId).update(member_id=memberId)
views:
from django.shortcuts import render,HttpResponse,redirect from app01.models import * from app01.utils.function import get_category import json from app01.utils.personCart import PersonCart from app01.utils.personRedis import Redis def index(request): #获取用户id,用户名 # member_session_id=request.session.get('memberId') # # member_session_name=request.session.get('memberName') from app01.utils.personSession import PersonSession person = PersonSession(request) member_session_id = person.session.get('memberId') member_session_name = person.session.get('memberName') if (request.COOKIES is not None) and (member_session_id is not None): # 检查是否以前有浏览者登陆 checkPerson(request,member_session_id) if member_session_id is None: member_session_id = 16 # 拿取商品列表(缓存中) redis = Redis() goods1 = redis.get('goods1') if goods1 is None: goods1=Goods.objects.filter(category__category_pid=1)[:4] redis.set('goods1', goods1) goods2 = redis.get('goods2') if goods2 is None: goods2 = Goods.objects.filter(category__category_pid=2)[:4] redis.set('goods2', goods2) goods3 = redis.get('goods3') if goods1 is None: goods3 = Goods.objects.filter(category__category_pid=3)[:4] redis.set('goods3', goods3) goods4 = redis.get('goods4') if goods4 is None: goods4 = Goods.objects.filter(category__category_pid=4)[:4] redis.set('goods4', goods4) goods5 = redis.get('goods5') if goods1 is None: goods5 = Goods.objects.filter(category__category_pid=5)[:4] redis.set('goods5', goods5) goods6 = redis.get('goods6') if goods6 is None: goods6 = Goods.objects.filter(category__category_pid=6)[:4] redis.set('goods6',goods6) # 拿取商品种类 category1=Category.objects.filter(category_name='新鲜水果').first() category2=Category.objects.filter(category_name='海产水产').first() category3=Category.objects.filter(category_name='猪牛羊肉').first() category4=Category.objects.filter(category_name='禽类蛋类').first() category5=Category.objects.filter(category_name='新鲜蔬菜').first() category6=Category.objects.filter(category_name='速冻食品').first() #商品字典 GOODSS = { category1:goods1, category2:goods2, category3:goods3, category4:goods4, category5:goods5, category6:goods6 } cart_num = 0 cart_nums = Cart.objects.filter(member_id=member_session_id) for i in cart_nums: cart_num += int(i.cart_num) # 商品多级联动 category_objs = Category.objects.filter(category_pid=0) category_objs_san = get_category(category_objs) return render(request,'app01_index.html',locals()) # 检查浏览者登陆了 def checkPerson(request,memberId): ''' :return: ''' # 1 把浏览者购物添加到用户购物车里 # 2 并删除浏览者购物车 # 3 清道夫 cartIdList = request.COOKIES.get('cartIdList') # 1 把浏览者购物添加到用户购物车里 PersonCart.addCart(cartIdList,memberId) # 2 并删除浏览者购物车 rep = HttpResponse() rep.delete_cookie('cartIdList') # 3 清道夫 Cart.objects.filter(member_id=16).delete() Cart.objects.filter(member_id=None).delete()