def generate_static_sku_detail_html(sku): """ 生成指定sku商品的详情页面 :param sku_id: :return: """ # 查询商品频道分类 categories = get_categories() # 查询当前商品规格 goods, sku, specs = get_goods_and_spec(sku.id) # 1. 获得模板 template = loader.get_template('detail.html') # 2. 设置参数 context = { 'categories': categories, 'goods': goods, 'sku': sku, 'specs': specs } # 3. 渲染页面 page = template.render(context) # 4. 写入文件 file_path = os.path.join(settings.STATIC_FILE_PATH, 'goods/%d.html' % sku.id) with open( file_path, 'w', ) as f: f.write(page)
def generate_static_index_html(): """ 生成静态的主页html文件 """ print('%s: generate_static_index_html' % time.ctime()) # 商品频道及分类菜单 categories = get_categories() # 广告内容 contents = {} # 获取所有的广告分类 content_categories = ContentCategory.objects.all() for cat in content_categories: contents[cat.key] = cat.content_set.filter(status=True) # 渲染模板 context = { 'categories': categories, 'contents': contents } # django 提供的视图加载器loader获取模板内容 template = loader.get_template('index.html') html_text = template.render(context) file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'index.html') with open(file_path, 'w') as f: f.write(html_text)
def get(self, request): # 商品分类页 from apps.goods.utils import get_categories categories = get_categories() # 查询所有广告类别 contents = {} content_categories = ContentCategory.objects.all() for cat in content_categories: contents[cat.key] = cat.content_set.filter( status=True).order_by('sequence') # 轮播图 # 快讯 # 页头广告 # 渲染模板的上下文 context = { "categories": categories, "contents": contents, } return render(request, "index.html", context)
def generate_static_index_html(): """ 生成静态的主页html文件 :return: """ categories = get_categories() # 广告内容 contents = {} content_categories = ContentCategory.objects.all() for cat in content_categories: contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence') # 渲染模板 context = { 'categories': categories, 'contents': contents } # 获取模板文件 template = loader.get_template("index.html") # 渲染首页的html字符串 html_text = template.render(context) # 将首页html字符串写入指定的目录,叫“index.html” file_path = os.path.join(settings.STATIC_GENERIC_HTML,"index.html") with open(file_path, "w", encoding="utf-8") as file: file.write(html_text)
def generate_static_sku_detail_html(sku): # 查询商品频道分类 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') file_path = os.path.join(settings.STATIC_GENERIC_HTML, 'detail/' + str(sku.id) + '.html') with open(file_path, 'w', encoding='utf-8') as f: f.write(html_text)
def get(self, request, sku_id): try: sku = models.SKU.objects.get(id=sku_id) except models.SKU.DoesNotExist: return render(request, '404.html') # 查询商品频道分类 categories = get_categories() # 查询面包屑导航 breadcrumb = get_breadcrumb(sku.category) # 渲染页面 context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sku': sku, } return render(request, "detail.html", context)
def get(self, request, category_id, page_num): try: category = models.GoodsCategory.objects.get(id=category_id) except models.GoodCategory.DoesNotExist: return http.HttpResponseNotFound('GoodsCategory does not exist') # 接收sort参数:如果用户不传,就是默认的排序规则 sort = request.GET.get("sort", "default") # 查询商品频道分类 categories = get_categories() # 查询面包屑导航 breadcrumb = get_breadcrumb(category) # 按照排序规则查询该分类商品SKU信息 if sort == "price": # 按照价格由低到高 sort_field = "price" elif sort == "hot": # 按照销量由高到低 sort_field = "-sales" else: # 'price'和'sales'以外的所有排序方式都归为'default' sort_field = 'create_time' skus = models.SKU.objects.filter(category=category, is_launched=True).order_by(sort_field) paginator = Paginator(skus, 5) page_skus = paginator.page(page_num) total_page = paginator.num_pages context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sort': sort, # 排序字段 'category': category, # 第三级分类 'page_skus': page_skus, # 分页后数据 'total_page': total_page, # 总页数 'page_num': page_num, # 当前页码 } return render(request, "list.html", context)