Example #1
0
    def get(self, request):
        user = request.user
        types = GoodsType.objects.all()
        #轮播类
        good_banners = IndexGoodsBanner.objects.all().order_by('index')
        #活动类
        promotion_banners = IndexPromotionBanner.objects.all().order_by(
            'index')

        for type in types:
            image_banners = IndexTypeGoodsBanner.objects.filter(
                type=type, display_type=1).order_by('index')
            title_banners = IndexTypeGoodsBanner.objects.filter(
                type=type, display_type=0).order_by('index')

            #动态添加标题和图片属性
            type.image_banners = image_banners
            type.title_banners = title_banners

        if user.is_authenticated:
            cart_count = get_cart_count(user)
        else:
            cart_count = 0

        context = {
            'titel': '首页',
            'user': user,
            'types': types,
            'good_banners': good_banners,
            'promotion_banners': promotion_banners,
            'cart_count': cart_count
        }
        return render(request, 'index.html', context)
Example #2
0
    def get(self, request, typeid, pageid, sort):
        """
            url中的参数依次代表
            typeid:商品类型;
            pageid:页数
            sort:查询条件,1默认,2根据价格查询,3根据点击量查询
            """
        # 获取最新发布的商品
        new_skus = GoodsSKU.objects.all().order_by('-create_time')[:2]
        # 根据条件查询所有商品
        if sort == '1':  # 按
            sumGoodList = GoodsSKU.objects.filter(type=typeid)
        elif sort == '2':  # 按价格从低到高
            sumGoodList = GoodsSKU.objects.filter(
                type=typeid).order_by('price')
        elif sort == '3':  # 按人气
            sumGoodList = GoodsSKU.objects.filter(
                type=typeid).order_by('-sales')
        # 分页
        paginator = Paginator(sumGoodList, 1)
        goodList = paginator.page(int(pageid))
        pindexlist = paginator.page_range

        goodtype = GoodsType.objects.get(id=typeid)
        types = GoodsType.objects.all()

        # 购物车数量
        user = request.user
        if user.is_authenticated:
            cart_count = get_cart_count(user)
        else:
            cart_count = 0

        context = {
            'list': 1,
            'guest_cart': 1,
            'goodtype': goodtype,
            'new_skus': new_skus,
            'goodList': goodList,
            'typeid': typeid,
            'sort': sort,
            'pindexlist': pindexlist,
            'pageid': int(pageid),
            'cart_count': cart_count,
            'types': types,
        }

        # 渲染返回结果
        return render(request, 'goods/list.html', context)
Example #3
0
    def get(self, request, goods_id):
        '''显示商品详细页面'''
        try:
            sku = GoodsSKU.objects.get(id=goods_id)
        except GoodsSKU.DoesNotExist:
            #商品不存在
            return redirect(reverse('goods:index'))

        #获取商品的分类信息
        types = GoodsType.objects.all()
        #获取商品评论后,后期扩展
        #获取新品信息
        new_skus = GoodsSKU.objects.filter(
            type=sku.type).order_by('-create_time')[:2]

        #获取同一个SPU的其他规格商品,后期扩展
        same_spu_skus = GoodsSKU.objects.filter(goods=sku.goods).exclude(
            id=goods_id)

        #如果用户已经登录
        user = request.user
        if user.is_authenticated():
            #添加用户的历史记录

            #链接redis
            conn = StrictRedis('192.168.12.189')
            #key
            history_key = 'history_%d' % user.id
            #移除列表中的goods_id
            conn.lrem(history_key, 0, goods_id)
            #把goods_id插入到列表的左侧
            conn.lpush(history_key, goods_id)
            #只保存用户浏览的5条信息
            conn.ltrim(history_key, 0, 4)

        #获取用户购物车商品的数目
        cart_count = get_cart_count(request.user)
        #组织模板上下文
        context = {
            'sku': sku,
            'types': types,
            'new_skus': new_skus,
            'cart_count': cart_count,
            'same_spu_skus': same_spu_skus,
        }
        # 使用模板
        return render(request, 'detail.html', context)
Example #4
0
    def get(self, request, goods_id):
        login_name = request.session.get('login_name', '')

        try:
            sku = GoodsSKU.objects.get(id=goods_id)
        except GoodsSKU.DoesNotExist:
            return redirect(reverse('food:index'))

        #新品推荐
        new_skus = GoodsSKU.objects.filter(type=sku.type).exclude(
            id=goods_id).order_by('-create_time')[:2]

        #获取同一个SPU的其他规格商品,后期拓展
        same_spu_skus = GoodsSKU.objects.filter(goods=sku.goods).exclude(
            id=goods_id)

        user = request.user
        #购物车
        cart_count = 0
        #如果用户已经登录
        if user.is_authenticated:
            cart_count = get_cart_count(user)

            #添加用户的历史记录
            conn = settings.REDIS_CONN
            history_key = 'history_%d' % user.id
            #移除列表中的goods_id
            conn.lrem(history_key, 0, goods_id)
            #把goods_id插入到列表的左侧
            conn.lpush(history_key, goods_id)
            #只保存用户最新浏览的5条信息
            conn.ltrim(history_key, 0, 4)

        context = {
            'title': '商品详情',
            'login_name': login_name,
            'sku': sku,
            'new_skus': new_skus,
            'cart_count': cart_count
        }
        return render(request, 'detail.html', context)
Example #5
0
    def get(self, request, sku_id):
        context = {}
        try:
            sku = GoodsSKU.objects.get(id=sku_id)
        except:
            return redirect('/')
        context['sku'] = sku

        # 新品推荐
        new_skus = GoodsSKU.objects.exclude(id=sku_id).filter(
            type=sku.type).order_by('-create_time')[:2]
        context['new_skus'] = new_skus

        # 其他规格
        same_spu_skus = GoodsSKU.objects.exclude(id=sku_id).filter(
            type=sku.type)
        context['same_spu_skus'] = same_spu_skus

        sku_images = sku.goodsimage_set.all()
        context['sku_images'] = sku_images

        # 用户浏览记录
        user = request.user
        if user.is_authenticated():
            # redis连接

            # 拼接key
            history_key = 'history_%s' % user.id
            # 如果已浏览过则从redis中移除
            redis_conn.lrem(history_key, 0, sku_id)
            # 添加
            redis_conn.lpush(history_key, sku_id)
            # 购物车数量
            cart_count = get_cart_count(user)
        else:
            cart_count = 0
        context['cart_count'] = cart_count
        types = GoodsType.objects.all()
        context['types'] = types

        return render(request, 'goods/detail.html', context)
Example #6
0
    def get(self, request):
        '''显示首页'''
        context = cache.get('cache_index')
        if context == None:
            print('设置换缓存')
            # 获取商品的种类信息
            types = GoodsType.objects.all()
            # 获取首页轮播商品信息
            goods_banners = IndexGoodsBanner.objects.all().order_by('index')
            # 获取首页促销活动信息
            promotion_banners = IndePromotionBanner.objects.all().order_by(
                'index')

            # 获取首页分类商品展示信息
            for type in types:  # GoodsType
                # 获取type种类首页分类的商品的图片展示信息
                image_banners = IndexTypeGoodsBanner.objects.filter(
                    type=type, display_type=1).order_by('index')
                # 获取type种类首页分类的商品的文字展示信息
                title_banners = IndexTypeGoodsBanner.objects.filter(
                    type=type, display_type=0).order_by('index')

                # 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
                type.image_banners = image_banners
                type.title_banners = title_banners

            # 组织模板上下文
            context = {
                'types': types,
                'goods_banners': goods_banners,
                'promotion_banners': promotion_banners,
            }

            cache.set('cache_index', context, 3600)
        # 获取用户购物车中商品的数目,暂时设置为0,待完善
        cart_count = get_cart_count(request.user)
        context.update(cart_count=cart_count)

        # 使用模板
        return render(request, 'index.html', context)
Example #7
0
    def get(self, request):

        context = cache.get('cache_index_page_data')
        if context == None:
            # 商品种类
            types = GoodsType.objects.all()

            # 首页轮播
            index_banner = IndexGoodsBanner.objects.all().order_by('index')

            # 首页促销
            promotion_banner = IndexPromotionBanner.objects.all().order_by(
                'index')

            # 首页分类展示详情
            for type in types:
                type.title_banner = IndexTypeGoodsBanner.objects.filter(
                    type=type, display_type=0).order_by('index')
                type.image_banner = IndexTypeGoodsBanner.objects.filter(
                    type=type, display_type=1).order_by('index')

            context = {
                'types': types,
                'index_banner': index_banner,
                'promotion_banner': promotion_banner,
            }

            cache.set('cache_index_page_data', context, 3600)

        # 购物车数量
        user = request.user
        if user.is_authenticated:
            cart_count = get_cart_count(user)
        else:
            cart_count = 0
        context.update(cart_count=cart_count)

        return render(request, 'goods/index.html', context)
Example #8
0
    def get(self, request, type_id, page):
        '''显示列表页'''
        #获取种类信息
        try:
            type = GoodsType.objects.get(id=type_id)
        except GoodsType.DoesNotExist:
            #种类不存在
            return redirect(reverse('goods:index'))

        #获取商品的分类信息
        types = GoodsType.objects.all()
        #读取排序方式,获取分类商品的信息
        #sort=default 按照默认id排序
        #sort=price 按照商品价格排序
        #sort=hot 按照商品销量排序
        sort = request.GET.get('sort')

        if sort == 'price':
            skus = GoodsSKU.objects.filter(type=type).order_by('price')
        elif sort == 'hot':
            skus = GoodsSKU.objects.filter(type=type).order_by('-sales')
        else:
            sort = 'default'
            skus = GoodsSKU.objects.filter(type=type).order_by('-id')

        #对数据进行分页
        paginator = Paginator(skus, 2)
        #获取page页的内容
        try:
            page = int(page)
        except Exception as e:
            page = 1

        if page > paginator.num_pages:
            page = 1

        #获取page页的Page实例对象
        skus_page = paginator.page(page)

        #进行页码的控制,页面上最多显示5个页面

        #1.总页数小于5页,页面上显示所有页面
        #2.如果当前页是前三页,显示1-5页
        #3.如果当前页是后三页,显示后5页
        #4.其他情况,显示当前页的钱2页,当前页,当前页的后2页
        num_pages = paginator.num_pages
        if num_pages < 5:
            pages = range(1, num_pages + 1)
        elif page <= 3:
            pages = range(1, 6)
        elif num_pages - page <= 2:
            pages = range(num_pages - 4, num_pages + 1)
        else:
            pages = range(page - 2, page + 3)
        #获取新的信息
        new_skus = GoodsSKU.objects.filter(
            type=type).order_by('-create_time')[:2]

        # 获取用户购物车商品的数目
        cart_count = get_cart_count(request.user)
        # 组织模板上下文
        context = {
            'type': type,
            'types': types,
            'skus_page': skus_page,
            'new_skus': new_skus,
            'cart_count': cart_count,
            'pages': pages,
            'sort': sort,
        }
        # 使用模板
        return render(request, 'list.html', context)