Example #1
0
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)

    # 构建当前商品的规格键
    sku_specs = sku.specs.order_by('spec_id')
    sku_key = []
    for spec in sku_specs:
        sku_key.append(spec.option.id)
    # 获取当前商品的所有SKU
    skus = sku.spu.sku_set.all()
    # 构建不同规格参数(选项)的sku字典
    spec_sku_map = {}
    for s in skus:
        # 获取sku的规格参数
        s_specs = s.specs.order_by('spec_id')
        # 用于形成规格参数-sku字典的键
        key = []
        for spec in s_specs:
            key.append(spec.option.id)
        # 向规格参数-sku字典添加记录
        spec_sku_map[tuple(key)] = s.id
    # 获取当前商品的规格信息
    goods_specs = sku.spu.specs.order_by('id')
    # 若当前sku的规格信息不完整,则不再继续
    if len(sku_key) < len(goods_specs):
        return
    for index, spec in enumerate(goods_specs):
        # 复制当前sku的规格键
        key = sku_key[:]
        # 该规格的选项
        spec_options = spec.options.all()
        for option in spec_options:
            # 在规格参数sku字典中查找符合当前规格的sku
            key[index] = option.id
            option.sku_id = spec_sku_map.get(tuple(key))
        spec.spec_options = spec_options

    # 上下文
    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(settings.STATICFILES_DIRS[0], 'detail/' + str(sku_id) + '.html')
    with open(file_path, 'w') as f:
        f.write(html_text)
Example #2
0
 def get(self, request, sku_id):
     """提供商品详情页"""
     # 接收和校验参数
     try:
         sku = SKU.objects.get(id=sku_id)
     except SKU.DoseNotExist:
         # return http.HttpResponseNotFound('sku_id 不存在')
         return render(request, '404.html')
     # 查询商品分类
     categories = get_categories()
     # 查询商品面包屑导航
     breadcrumb = get_breadcrumb(sku.category)
     # 构建当前商品的规格键
     sku_specs = sku.specs.order_by('spec_id')
     sku_key = []
     for spec in sku_specs:
         sku_key.append(spec.option.id)
     # 获取当前商品的所有SKU
     skus = sku.spu.sku_set.all()
     # 构建不同规格参数(选项)的sku字典
     spec_sku_map = {}
     for s in skus:
         # 获取sku的规格参数
         s_specs = s.specs.order_by('spec_id')
         # 用于形成规格参数-sku字典的键
         key = []
         for spec in s_specs:
             key.append(spec.option.id)
         # 向规格参数-sku字典添加记录
         spec_sku_map[tuple(key)] = s.id
     # 获取当前商品的规格信息
     goods_specs = sku.spu.specs.order_by('id')
     # 若当前sku的规格信息不完整,则不再继续
     if len(sku_key) < len(goods_specs):
         return
     for index, spec in enumerate(goods_specs):
         # 复制当前sku的规格键
         key = sku_key[:]
         # 该规格的选项
         spec_options = spec.options.all()
         for option in spec_options:
             # 在规格参数sku字典中查找符合当前规格的sku
             key[index] = option.id
             option.sku_id = spec_sku_map.get(tuple(key))
         spec.spec_options = spec_options
     # 构造上下文
     context = {
         'categories': categories,
         'breadcrumb': breadcrumb,
         'sku': sku,
         'specs': goods_specs
     }
     return render(request, 'detail.html', context)
Example #3
0
    def get(self, request, category_id, page_num):
        '''查询并渲染商品列表页'''
        # 校验category_id的范围:11111111111
        try:
            # 三级类别
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('参数category_id不存在')
        # 获取sort(排序规则):如果sort没有值,取default
        sort = request.GET.get('sort', 'default')
        # 根据sort选择排序字段,排序字段必须是模型类的属性
        if sort == 'price':
            sort_field = 'price'  # 按照价格由低到高排序
        elif sort == 'hot':
            sort_field = '-sales'  # 按照销量从高到低排序
        else:  # 只要不是'price'和'sales'其他的所有情况都归为'default'
            sort = 'default'  # 当出现了?sort=itcast 也把sort设置为'default'
            sort_field = 'create_time'
        # 查询商品分类
        categories = get_categories()
        # 查询面包屑导航:一级 -> 二级 -> 三级
        breadcrumb = get_breadcrumb(category)
        # 分页和排序查询:category查询sku,一查多,一方的模型对象,多方关联字段.all/filter
        # skus = SKU.objects.filter(category=category,is_launcher=True)   # 无经验查询
        # skus = SKU.objects.filter(category_id=category_id,is_launcher=True)   # 无经验查询
        # skus = category.sku_set.filter(is_launcher=True).order_by('排序字段:create_time,price,-sales')  # 有经验的查询
        skus = category.sku_set.filter(is_launched=True).order_by(
            sort_field)  # 有经验的查询

        # 创建分页器
        # Paginator('要分页的记录','每页记录的条数')
        paginator = Paginator(skus, 5)
        # 获取用户当前要看的数据
        try:
            page_skus = paginator.page(page_num)
        except EmptyPage:
            return http.HttpResponseNotFound('Empty Page')
        # 获取总页数:前端的插件需要使用
        total_page = paginator.num_pages
        # 构造上下文
        context = {
            'categories': categories,
            'breadcrumb': breadcrumb,
            'page_skus': page_skus,
            'total_page': total_page,
            'page_num': page_num,
            'sort': sort,
            'category_id': category_id,
        }
        return render(request, 'list.html', context)
Example #4
0
 def get(self, request):
     '''提供首页广告页面'''
     # 准备商品分类对应的字典
     categories = OrderedDict()
     # TODO 需要中重点复习的内容
     # # 查询并展示商品分类:37个一级类别
     # channels = GoodsChannel.objects.order_by('group_id', 'sequence')
     # # 遍历所有频道
     # for channel in channels:
     #     # 获取当前频道所在的组
     #     group_id = channel.group_id  # 当前组
     #     # 构造基本的数据框架:只有11个组
     #     if group_id not in categories:
     #         categories[group_id] = {'channels': [], 'sub_cats': []}
     #     cat1 = channel.category  # 当前频道的类别
     #     # 将cat1添加到channels
     #     categories[group_id]['channels'].append({
     #         'id': cat1.id,
     #         'name': cat1.name,
     #         'url': channel.url
     #     })
     #     # 查询二级和三级
     #     for cat2 in cat1.subs.all():  # 从一级类别找二级类别
     #         cat2.sub_cats = []  # 给二级类别添加一个保存三级类别的列表
     #         for cat3 in cat2.subs.all():  # 从二级类别找三级类别
     #             cat2.sub_cats.append(cat3)  # 将三级类别添加到二级类别sub_cats
     #
     #         # 将二级类别添加到一级类别的sub_cats
     #         categories[group_id]['sub_cats'].append(cat2)
     categories = get_categories()
     # TODO !!!!!![代码中的FastDFS启动的storage没有报错,图片也可以正常得到,但是python.manage.shell中创建client不能使用,未解决!]
     # 查询首页广告的数据
     # 查询所有广告的类别
     contents = OrderedDict()
     content_categories = ContentCategory.objects.all()
     for content_category in content_categories:
         contents[content_category.key] = content_category.content_set.filter(status=True).order_by(
             'sequence')  # 查询到未下架的广告并排序
     # 使用广告类别查询出该类别对应的所有广告内容
     # 构造上下文
     context = {
         'categories': categories,
         'contents': contents
     }
     return render(request, 'index.html', context=context)
Example #5
0
def generate_static_index_html():
    '''静态化首页'''
    # 查询首页数据
    # 查询并展示商品分类
    categories = get_categories()
    # 查询所有广告类别
    contents = OrderedDict()
    content_categories = ContentCategory.objects.all()
    for content_category in content_categories:
        contents[content_category.key] = content_category.content_set.filter(
            status=True).order_by('sequence')  # 查询到未下架的广告并排序
    # 使用广告类别查询出该类别对应的所有广告内容
    # 构造上下文
    context = {'categories': categories, 'contents': contents}
    # 渲染模板
    # 先使用模板文件
    template = loader.get_template('index.html')
    # 再使用上下文渲染模板文件
    html_text = template.render(context)
    # 将模板文件写入静态路径
    file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)