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)
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)
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 SKU.DoesNotExist: 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)
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)