def get(self,request,category_id,page_num): #1,获取分类信息 categories = get_categories() #2,获取分类对象 category = GoodsCategory.objects.get(id=category_id) #拼接数据,返回响应 context = { "categories":categories, "category":category } return render(request,'list.html',context=context)
def get(self, request): #1,获取分类信息 categories = get_categories() #4,获取手机分类广告数据 contents = {} content_categories = ContentCategory.objects.order_by("id") for content_category in content_categories: contents[content_category. key] = content_category.content_set.order_by("sequence") #拼接数据,渲染页面 context = {"categories": categories, "contents": contents} return render(request, 'index.html', context=context)
def get(self, request): #1,获取分类信息 categories = get_categories() #4,拼接广告数据 contents = {} content_catetories = ContentCategory.objects.all() for content_catetory in content_catetories: contents[content_catetory. key] = content_catetory.content_set.order_by('sequence') #5,拼接数据,返回响应 context = {"categories": categories, "contents": contents} return render(request, 'index.html', context=context)
def get(self,request): categories = get_categories() # 定义字典 # 获取频道 #遍历频道 #获取频道的组好 #组装数据 #bian'li # # 商品频道及分类菜单[先查询一级分类[分组],接着查询二级,最后是三级] # categories = {} # # 创建一个字典,categories ={} 表示频道和分类 # #分类频道组, # channels = GoodsChannel.objects.order_by('group_id','sequence') # for channel in channels: # group_id = channel.group_id # 找到当前频道组 # #找到当前组 # if group_id not in categories: #如果categories里面没有这个组,就创建频道组字典 # categories[group_id] ={"channels":[], "sub_cats":[]} # #因为一级分类和频道有各自需要的信息,所以channels里面的内容需要两者结合 # categories[group_id]['channels'].append({ # "id":channel.category.id, # "name":channel.category.name, # 'url':channel.url # }) # for cat2 in channel.category.subs.all():#查询所有子类 # cat2.sub_cats=[]#添加一个属性关于子类的 # for cat3 in cat2.subs.all(): # cat2.sub_cats.append(cat3) # categories[group_id]['sub_cats'].append(cat2) contents = {} content_category = ContentCategory.objects.order_by('id') for category in content_category: contents[category.key] = category.content_set.all() context = { "categories": categories, "contents":contents } return render(request,'index.html',context=context)
def get(self, request, category_id, page_num): #1,获取分类信息,和过滤参数 categories = get_categories() sort_field = request.GET.get("sort", "default") #1,1 判断排序的字段 if sort_field == "price": sort = "-price" elif sort_field == "hot": sort = "-sales" else: sort = "-create_time" #2,查询sku数据 skus = SKU.objects.filter(category_id=category_id).order_by(sort) #3,分页处理 paginate = Paginator(object_list=skus, per_page=5) page = paginate.page(page_num) current_page = page.number #当前页 skus_list = page.object_list #当前页对象列表 total_page = paginate.num_pages #总页数 #3,获取分类对象 category = GoodsCategory.objects.get(id=category_id) #拼接数据返回响应 context = { "categories": categories, "skus": skus_list, "category": category, "current_page": current_page, "total_page": total_page, "sort": sort_field } return render(request, 'list.html', context=context)
def generate_static_detail_html(sku_id): # 1,获取分类数据 categories = get_categories() # 2,获取面包屑数据 category = get_crumbs(sku_id) # 3,查询商品sku对象 sku = SKU.objects.get(id=sku_id) # 4,商品sku规格信息 # 构建当前商品的规格键 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, "category": category, "sku": sku, "specs": goods_specs } #1,获取详情页模板 template = loader.get_template('detail.html') html_text = template.render(context) #2,获取文件路径 file_path = os.path.join(settings.STATICFILES_DIRS[0], 'detail/' + str(sku_id) + '.html') #3,将数据写入到路径中 with open(file_path, 'w', encoding='utf-8') as f: f.write(html_text)