def generate_static_sku_detail_html(sku_id):
    """
    生成静态商品详情页面
    :param sku_id: 商品sku id
    """
    # 获取当前sku的信息
    sku = models.SKU.objects.get(id=sku_id)

    # 查询商品频道分类
    categories = get_categories()
    # 查询面包屑导航
    breadcrumb = get_breadcrumb(sku.category)

    # 构建当前商品的规格键
    goods_specs = get_goods_specs(sku)

    # 上下文
    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': goods_specs,
    }

    template = loader.get_template('detail.html')
    html_text = template.render(context)
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR),
                             'front_end_pc/goods/' + str(sku_id) + '.html')
    with open(file_path, 'w') as f:
        f.write(html_text)
Exemple #2
0
    def get(self, request, category_id):
        """提供商品列表页"""
        # 获取参数:
        page = request.GET.get('page')
        page_size = request.GET.get('page_size')
        ordering = request.GET.get('ordering')

        # 判断category_id是否正确
        try:
            # 获取三级菜单分类信息:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return JsonResponse({'code': 400, 'errmsg': '获取mysql数据出错'})

        # 查询面包屑导航(函数在下面写着)
        breadcrumb = get_breadcrumb(category)

        # 排序方式:
        try:
            skus = SKU.objects.filter(category=category,
                                      is_launched=True).order_by(ordering)
        except Exception as e:
            return JsonResponse({'code': 400, 'errmsg': '获取mysql数据出错'})

        paginator = Paginator(skus, page_size)
        # 获取每页商品数据
        try:
            page_skus = paginator.page(page)
        except EmptyPage:
            # 如果page_num不正确,默认给用户400
            return JsonResponse({'code': 400, 'errmsg': 'page数据出错'})
        # 获取列表页总页数
        total_page = paginator.num_pages

        # 定义列表:
        list = []
        # 整理格式:
        for sku in page_skus:
            list.append({
                'id': sku.id,
                'default_image_url': sku.default_image.url,
                'name': sku.name,
                'price': sku.price
            })

        # 把数据变为 json 发送给前端
        return JsonResponse({
            'code': 0,
            'errmsg': 'ok',
            'breadcrumb': breadcrumb,
            'list': list,
            'count': total_page
        })
Exemple #3
0
    def get(self,request,sku_id):
        try:
            sku=SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            pass
        categories=get_categories()
        breadcrumb=get_breadcrumb(sku.category)
        goods_specs=get_goods_specs(sku)
        context={
            'categories': categories,
            'breadcrumb': breadcrumb,
            'sku': sku,
            'specs':goods_specs,
        }

        return render(request,'detail.html',context)
Exemple #4
0
    def get(self, request, sku_id):
        """
        1. 获取商品id
        2. 根据商品id查询商品信息
        3. 获取分类数据
        4. 获取面包屑数据
        5. 获取规格和规格选项数据
        6. 组织数据,进行HTML模板渲染
        7. 返回响应
        :param request:
        :param sku_id:
        :return:
        """
        # 1. 获取商品id
        # 2. 根据商品id查询商品信息
        try:
            sku = SKU.objects.get(id=sku_id)
        except:
            return JsonResponse({'code': 400, 'errmsg': '没有此商品'})

        # 3. 获取分类数据
        categories = get_categories()

        # 4. 获取面包屑数据
        # sku 有 三级分类属性
        breadcrumb = get_breadcrumb(sku.category)

        # 5. 获取规格和规格选项数据
        # 传递 sku对象
        specs = get_goods_specs(sku)

        # 6. 组织数据,进行HTML模板渲染
        # context 的key 必须按照课件来!!!
        # 因为模板已经写死了
        context = {
            'sku': sku,
            'categories': categories,
            'breadcrumb': breadcrumb,
            'specs': specs
        }

        # 7. 返回响应
        return render(request, 'detail.html', context)
Exemple #5
0
def generic_detail_html(sku):
    # try:
    #     sku=SKU.objects.get(id=sku_id)
    # except SKU.DoesNotExist:
    #     pass
    categories = get_categories()
    breadcrumb = get_breadcrumb(sku.category)
    goods_specs = get_goods_specs(sku)
    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': goods_specs,
    }
    import os
    from meiduo import settings
    from django.template import loader
    detail_template = loader.get_template('detail.html')

    detail_html_data = detail_template.render(context)
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR),
                             'front_end_pc/goods/%s.html' % sku.id)
Exemple #6
0
    def get(self, request, category_id):
        #获取前端的请求中排序的字段,用于排序
        ordering = request.GET.get('ordering')
        # 获取前端的请求中每页多少数据,用于分页
        page_size = request.GET.get('page_size')
        # 获取前端的请求中指定页数据
        page = request.GET.get('page')

        try:
            # 获取三级菜单分类信息:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return JsonResponse({'code': 400, 'errmsg': '获取mysql数据出错'})
        #获取面包屑数据
        breadcrumb = get_breadcrumb(category)
        #获取对应分类的sku数据,排序
        SKUS = SKU.objects.filter(category=category,
                                  is_launched=True).order_by(ordering)
        #分页
        paginator = Paginator(SKUS, per_page=page_size)

        page_skus = paginator.page(page)
        sku_list = []
        for sku in page_skus.object_list:
            sku_list.append({
                'id': sku.id,
                'default_image_url': sku.default_image.url,
                'name': sku.name,
                'price': sku.price
            })
        #获取总页数
        total_page = paginator.num_pages
        return JsonResponse({
            'code': 0,
            'errmsg': 'ok',
            'breadcrumb': breadcrumb,
            'list': sku_list,
            'count': total_page
        })
Exemple #7
0
    def get(self, request, category_id):
        # 1 接受请求
        data = request.GET
        # 2 提取参数
        page = data.get('page')
        page_size = data.get('page_size')
        ordering = data.get('ordering')
        # 3 根据分类id查询数据
        from apps.goods.models import GoodsCategory
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except:
            return JsonResponse({'code': 400, 'errmsg': "没有此分类"})

        # 4 验证参数
        # 5 查询数据
        from apps.goods.models import SKU
        skus = SKU.objects.filter(category=category, is_launched=True).order_by(ordering)
        # 6 分页数据
        from django.core.paginator import Paginator
        paginator = Paginator(skus, per_page=page_size)
        page_skus = paginator.page(page)
        # 获取分了多少页
        total_num = paginator.num_pages
        # 将对象列表转换成字典
        sku_list = []
        for item in page_skus:
            sku_list.append({
                'id': item.id,
                'name': item.name,
                'price': item.price,
                'default_image_url': item.default_image.url
            })
        from utils.goods import get_breadcrumb
        breadcrumb = get_breadcrumb(category)
        # 返回响应
        return JsonResponse({'code': 0, 'errmsg': "ok", 'list': sku_list, 'count': total_num, 'breadcrumb': breadcrumb})
Exemple #8
0
    def get(self,request,category_id):
        """
        1. 接收参数
        2. 提取参数
        3. 根据分类id查询分类数据
        4. 验证参数(省略)
            4.1 page 整数
            4.2 page_size 整数
            4.3 ordering 只能是 create_time,price,sales
        5. 查询数据
        6. 分页数据
        7. 将对象列表转换为字典列表
        8. 返回响应
        :param request:
        :param category_id:
        :return:
        """
        #list/115/skus/?page=1&page_size=5&ordering=-create_time
        # 1. 接收参数
        data=request.GET
        # 2. 提取参数
        page=data.get('page')               # 第几页
        page_size=data.get('page_size')     # 每页多少条数据
        ordering=data.get('ordering')       # 排序字段
        # 3. 根据分类id查询分类数据
        from apps.goods.models import GoodsCategory
        try:
            category=GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return JsonResponse({'code':400,'errmsg':'没有此分类'})
        # 4. 验证参数(省略)
        #     4.1 page 整数
        #     4.2 page_size 整数
        #     4.3 ordering 只能是 create_time,price,sales

        # 5. 查询数据
        # 查询的是具体某一个商品 SKU
        from apps.goods.models import SKU
        # 查询并且排序
        skus=SKU.objects.filter(category=category,is_launched=True).order_by(ordering)

        # 6. 分页数据
        # 导入分页类
        from django.core.paginator import Paginator
        # 创建分页实例
        # object_list,  列表数据
        # per_page      每页多少条数据

        paginator=Paginator(skus,per_page=page_size)
        # 获取指定页码的数据
        # page() 获取分页数据
        # page 查询参数
        page_skus=paginator.page(page)
        # 获取分了多少页
        total_num = paginator.num_pages


        # 7. 将对象列表转换为字典列表
        sku_list = []

        for item in page_skus:
            sku_list.append({
                'id':item.id,
                'name':item.name,
                'price':item.price,
                'default_image_url': item.default_image.url
            })

        # 获取面包屑数据
        from utils.goods import get_breadcrumb
        breadcrumb = get_breadcrumb(category)

        # 8. 返回响应
        return JsonResponse({'code':0,
                             'errmsg':'ok',
                             'list':sku_list,
                             'count':total_num,
                             'breadcrumb':breadcrumb})
Exemple #9
0
    def get(self, request, category_id):
        """
        1. 接收参数
        2. 提取参数
        3. 根据分类id查询分类数据
        4. 验证参数(省略)
        4.1 page 整数
        4.2 page_size 整数
        4.3 ordering 只能是 create_time,price,sales
        5. 查询数据
        6. 分页数据
        7. 将对象列表转换为字典列表
        8. 返回响应
        :param request:
        :param category_id:
        :return:
        """
        # 1. 接收参数
        # 2. 提取参数
        page = request.GET.get('page')
        page_size = request.GET.get('page_size')
        ordering = request.GET.get('ordering')

        # 3. 根据分类id查询分类数据
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except:
            return JsonResponse({"code": 400, 'errmsg': '没有此分类'})

        # 4. 验证参数(省略)
        # 4.1 page 整数
        # 4.2 page_size 整数
        # 4.3 ordering 只能是 create_time,price,sales
        # 5. 查询数据
        skus = SKU.objects.filter(category=category,
                                  is_launched=True).order_by(ordering)

        # 6. 分页数据
        paginator = Paginator(skus, per_page=page_size)
        page_skus = paginator.page(page)
        # 获取分了多少页
        total_num = paginator.num_pages

        sku_list = []
        # 7. 将对象列表转换为字典列表
        for item in page_skus:
            sku_list.append({
                'id': item.id,
                'name': item.name,
                'price': item.price,
                'default_image_url': item.default_image.url
            })

        # 获取面包屑数据
        breadcrumb = get_breadcrumb(category)

        # 8. 返回响应
        return JsonResponse({
            'code': 0,
            'errmsg': 'ok',
            'list': sku_list,
            'count': total_num,
            'breadcrumb': breadcrumb
        })