def get(self, request): # 频道分类信息 categories = get_category() # 查询广告数据 #1.查询广告位 contents = ContentCategory.objects.all() content_dict = {} #2.遍历,查询每个位置的广告信息 for content in contents: #3.将广告数据赋给指定的广告位 content_dict[content.key] = content.content_set.filter( status=True).order_by('sequence') context = {'categories': categories, 'contents': content_dict} return render(request, 'index.html', context)
def get(self, request, category_id, page_num): # 查询当前指定的分类对象 try: category3 = GoodsCategory.objects.get(pk=category_id) except: return render(request, '404.html') # 分类 categories = get_category() # 面包屑导航 breadcrumb = get_breadcrumb(category3) # 热销排行(后续通过Ajax实现) # 当前分类的库存商品 skus = category3.sku_set.filter(is_launched=True) # 排序 sort = request.GET.get('sort', 'default') if sort == 'price': # 价格 skus = skus.order_by('price') elif sort == 'hot': # 人气 skus = skus.order_by('-sales') else: # 默认 skus = skus.order_by('-id') # 分页 paginator = Paginator(skus, 5) # 将列表skus按照每页5条数据进行分页 page_skus = paginator.page(page_num) # 获取第page_num页的数据 total_page = paginator.num_pages # 总页数 context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sort': sort, 'page_skus': page_skus, 'category': category3, 'page_num': page_num, 'total_page': total_page } return render(request, 'list.html', context)
def generate_static_index_html(): # 查询生成html字符串 # 频道分类信息 categories = get_category() # 查询广告数据 # 1.查询广告位 contents = ContentCategory.objects.all() content_dict = {} # 2.遍历,查询每个位置的广告信息 for content in contents: # 3.将广告数据赋给指定的广告位 content_dict[content.key] = content.content_set.filter( status=True).order_by('sequence') response = render(None, 'index.html', { 'categories': categories, 'contents': content_dict }) html_str = response.content.decode() # 写文件 file_path = os.path.join(settings.BASE_DIR, 'static/index.html') with open(file_path, 'w') as f1: f1.write(html_str)
def get(self, request, sku_id): generate_static_detail_html.delay(sku_id) try: sku = SKU.objects.get(pk=sku_id) except: return render(request, '404.html') # - 频道分类 categories = get_category() # 面包屑导航 category3 = sku.category breadcrumb = get_breadcrumb(category3) # - 库存商品对象(根据主键查询) # 标准商品对象 spu = sku.spu # - 规格选项 # 当前库存商品的选项,如当前库存商品为15号,则当前规格选项信息为:[16,20]===>6,7 # list1=sku.specs.order_by('spec_id') # option_current=[] # for info in list1: # option_current.append(info.option_id) option_current = [ info.option_id for info in sku.specs.order_by('spec_id') ] # 查询所有的库存商品与选项信息 skus = spu.sku_set.filter(is_launched=True) ''' 当前数据,需要包含:库存商品编号,选项列表 当前已知:选项列表,找:库存商品编号 { 选项列表:库存商品编号 } dict1[键]===>值 ''' sku_option_dict = {} for sku_temp in skus: option_list = [ info.option_id for info in sku_temp.specs.order_by('spec_id') ] sku_option_dict[tuple(option_list)] = sku_temp.id ''' { (13,20):9, (13,21):10, ... } ''' # 规格==》选项===》链接 # 查询指定标准商品的所有规格 specs = spu.specs.all() # 6===>0,7=====>1 spec_list = [] for index, spec in enumerate(specs): # [20,5,8,39] spec_dict = {'name': spec.name, 'options': []} # 查询指定规格的所有选项 options = spec.options.all() # 遍历,加入规格字典的列表中 for option in options: # 根据当前选项获取新的完整选项,即保持其它规格的选项不变,只替换本规格的选项 option_current_temp = option_current[:] option_current_temp[ index] = option.id # [16,20]==>[13,20]===>16--->13 sku_id = sku_option_dict.get(tuple(option_current_temp), 0) spec_dict['options'].append({ 'name': option.value, 'selected': option.id in option_current, 'sku_id': sku_id }) spec_list.append(spec_dict) # - 热销排行 context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sku': sku, 'spu': spu, 'category_id': category3.id, 'spec_list': spec_list } return render(request, 'detail.html', context)