def get_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)
def generate_static_index_html(): # 测试 print("%s:generate_static_index_html" % time.ctime()) # 获取商品频道和分类 categories = get_categories() # 获取所有广告类别 content_category = ContentCategory.objects.all() contents ={} # 遍历广告类别 取出广告信息 for category in content_category: contents[category.key] = category.content_set.filter(status=True).order_by('sequence') # 渲染模板 context = { 'categories': categories, 'contents': contents } # 获取首页模板 template = loader.get_template('index.html') # 渲染成html_text html_text = template.render(context) # 将html_text写入指定目录 file_path = os.path.join(settings.STATICFILES_DIRS[0],'index.html') with open(file_path,'w',encoding='utf-8') as file: file.write(html_text)
def generate_static_index_html(): print('%s: generate_static_index_html' % time.ctime()) # 1.获取 首页需要的数据 from apps.contents.utils import get_categories categories = get_categories() # 2.获取广告数据 # 2.1 获取所有的广告分类 content_category from apps.contents.models import ContentCategory content_categories = ContentCategory.objects.all() # 2.2 遍历 广告分类 contents = {} for cat in content_categories: # 2.3 通过外键属性 获取 广告内容 contents[cat.key] = cat.content_set.filter( status=True).order_by('sequence') # 前端数据 context = {'categories': categories, 'contents': contents} # 2.获取 模板里面的 index.html from django.template import loader template = loader.get_template('index.html') # 3.渲染 index.html 和 数据 html_text = template.render(context) # 4. 写入本地文件 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)
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).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.STATICFILES_DIRS[0], 'index.html') with open(file_path, 'w', encoding='utf-8') as f: f.write(html_text)
def generate_static_index_html(): """ 定义:用户直接去静态服务器,访问处理好的静态html文件。 需求:为什么要静态化 减少数据库查询次数。 提升页面响应效率。 # 1.生成静态的主页html文件 # 2.获取频道和分类 # 3.获取广告内容 # 4.渲染模板 # 5.获取首页模板文件 :return: """ # 1.生成静态的主页html文件 print('%s:generate_static_index_html' % time.ctime()) # 2.获取频道和分类 categories = get_categories() # 3.获取广告内容 contents = {} content_categories = ContentCategory.objects.all() for cat in content_categories: contents[cat.key] = cat.content_set.filter( status=True).order_by('sequence') # 4.渲染模板 context = {'categories': categories, 'contents': contents} # 5.获取首页模板文件 template = loader.get_template('index.html') # 6.渲染首页html字符串(获取数据) html_text = template.render(context) #将首页的html字符串写入到制定目录,命名'index.html' 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)
def generate_static_index_html(): print('%s: generate_static_index_html' % time.ctime()) # 1.获取 首页 数据 交互数据库 categories = get_categories() # 2.获取 广告数据 contents = {} # 2.1 获取广告分类数据 所有 all() content_categorys = ContentCategory.objects.all() # 2.2 遍历 分类 id name key for cat in content_categorys: # 2.3 根据分类 获取 对应的 广告内容 contents[cat.key] = cat.content_set.filter( status=True).order_by('sequence') context = {'categories': categories, 'contents': contents} # 2.获取 首页 的 模板文件 template = loader.get_template('index.html') # 3.将数据和 模板文件 --渲染 html_text = template.render(context) # 4.写入本地---本地路径 file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html') with open(file_path, 'w') as f: f.write(html_text)
def get(self, request, sku_id): # 查看当前sku的信息 try: sku = SKU.objects.get(id=sku_id) except SKU.DoesNotExist: 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)
def get(self, request, sku_id): try: sku = SKU.objects.get(id=sku_id) except SKU.DoesNotExist: return render(request, '404.html') # 查询商品频道分类 categories = get_categories() # 查询面包屑导航 breadcrumb = get_breadcrumb(sku.category) # 查询当前sku所对应的spu spu = sku.spu # 获取当前sku所对应的三级分类 category = sku.category """1.准备当前商品的规格选项列表 [8, 11]""" # 获取出当前正显示的sku商品的规格选项id列表 current_sku_spec_qs = sku.specs.order_by('spec_id') current_sku_option_ids = [] # [8, 11] for current_sku_spec in current_sku_spec_qs: current_sku_option_ids.append(current_sku_spec.option_id) """2.构造规格选择仓库 {(8, 11): 3, (8, 12): 4, (9, 11): 5, (9, 12): 6, (10, 11): 7, (10, 12): 8} """ # 构造规格选择仓库 temp_sku_qs = spu.sku_set.all() # 获取当前spu下的所有sku # 选项仓库大字典 spec_sku_map = {} # {(8, 11): 3, (8, 12): 4, (9, 11): 5, (9, 12): 6, (10, 11): 7, (10, 12): 8} for temp_sku in temp_sku_qs: # 查询每一个sku的规格数据 temp_spec_qs = temp_sku.specs.order_by('spec_id') temp_sku_option_ids = [] # 用来包装每个sku的选项值 for temp_spec in temp_spec_qs: temp_sku_option_ids.append(temp_spec.option_id) spec_sku_map[tuple(temp_sku_option_ids)] = temp_sku.id """3.组合 并找到sku_id 绑定""" spu_spec_qs = spu.specs.order_by('id') # 获取当前spu中的所有规格 for index, spec in enumerate(spu_spec_qs): # 遍历当前所有的规格 spec_option_qs = spec.options.all() # 获取当前规格中的所有选项 temp_option_ids = current_sku_option_ids[:] # 复制一个新的当前显示商品的规格选项列表 for option in spec_option_qs: # 遍历当前规格下的所有选项 temp_option_ids[index] = option.id # [8, 12] option.sku_id = spec_sku_map.get(tuple(temp_option_ids)) # 给每个选项对象绑定下他sku_id属性 spec.spec_options = spec_option_qs # 把规格下的所有选项绑定到规格对象的spec_options属性上 context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sku': sku, 'spu': spu, 'category': category, 'spec_qs': spu_spec_qs } return render(request, 'detail.html', context)
def get(self, request): # 查询商品频道和分类 categories = get_categories() return render(request, 'index.html', { 'categories': categories, 'contents': self.get_advertising() })
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, } # 获取详情页模板 templage = loader.get_template('detail.html') # 渲染成html_text html_text = templage.render(context) # 将html_text写入指定文件 file_path = os.path.join(settings.STATICFILES_DIRS[0], 'detail/' + str(sku.id) + '.html') with open(file_path, 'w', encoding='utf-8') as file: file.write(html_text)
def get(self, request, sku_id): """ 1.根据sku_id,进行查询 2.获取面包屑 3.获取分类数据 """ # 1.根据sku_id,进行查询 try: sku = SKU.objects.get(pk=sku_id) except Exception as e: return render(request, '404.html') # 2.获取面包屑 breadcrumb = get_breadcrumb(sku.category) # 3.获取分类数据 categories = get_categories() # 构建当前商品的规格键 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=context)
def get(self, request, category_id, page_num): # 判断有没有三级分类数据 try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: logger.error(e) return http.HttpResponseNotFound('GoodsCategory does not exist') # 调用商品频道分类分数 categories = get_categories() # 调用面包屑导航 breadcrumb = get_breadcrumb(category) # 按照排序规则查询该分类商品SKU信息 # 获取用户的查询方式 sort = request.GET.get('sort', 'default') # 判断 if sort == 'price': sort_field = 'price' elif sort == 'hot': sort_field = '-sales' else: sort_field = 'create_time' # SKU信息排序 skus = SKU.objects.filter(category=category, is_launched=True).order_by(sort_field) # 分页 from django.core.paginator import Paginator # 实例化分页器 提供内容和分页的数 pagenator = Paginator(skus, constants.GOODS_LIST_LIMIT) # 获取总页数 total_page = pagenator.num_pages # 获取每页的数据 try: page_skus = pagenator.page(page_num) except Exception as e: logger.error(e) return http.HttpResponseNotFound('empty page') # 渲染页面 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)
def get(self, request, category_id, page_num): ''' 商品三级列表 :param request: :param category_id: :param page_num: :return: ''' """提供商品列表页""" # 判断category_id是否正确 try: category = models.GoodsCategory.objects.get(id=category_id) except models.GoodsCategory.DoesNotExist: return HttpResponseNotFound('GoodsCategory does not exist') # 1.0 三级列表 categories = get_categories() # 2.0 面包屑 # 接收sort参数:如果用户不传,就是默认的排序规则 sort = request.GET.get('sort', 'default') # 默认 if sort == 'price': # 升序 价格从低到高 sort_field = 'price' elif sort == 'hot': # 降序 热度从搞到低 sort_field = '-sales' else: # 其余的都是默认 sort_field = 'create_time' skus = SKU.objects.filter(category=category, is_launched=True).order_by(sort_field) # 实例化创建分页器 每页N个数据 # 所有数据 paginator = Paginator(skus, constants.GOODS_LIST_LIMIT) # 获取每页商品数据 try: page_skus = paginator.page(page_num) except EmptyPage: # 如果page_num不正确,默认给用户404 return HttpResponseNotFound('empty page') # 获取列表页总页数 total_page = paginator.num_pages breadcrumb = get_breadcrumb(category) 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)
def get(self, request, sku_id): # 1.根据sku_id获取当前sku的信息 try: sku = SKU.objects.get(id=sku_id) except Exception as e: # todo 如果搜索的商品不存在,直接跳转商品不存在的404页面 return render(request, '404.html') # 2.查询商品频道分类 categories = get_categories() # 3.查询面包屑导航 breadcrumb = get_breadcrumb(sku.category) """多对多的信息""" # 4.构建当前商品的规格键 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 # 5.组织渲染页面的数据 context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sku': sku, 'specs': goods_specs, } return render(request, 'detail.html', context)
def get(self, request, category_id, page_num): # 获取三级分类的对象 try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: return HttpResponseNotFound('商品分类不存在') # 1.商品分类数据 from apps.contents.utils import get_categories categories = get_categories() # 2.面包屑组件数据 from apps.goods.utils import get_breadcrumb breadcrumb = get_breadcrumb(category) # 3. 排序 # 3.1 获取sort 里面的参数值 查询参数 sort = request.GET.get('sort', 'default') # 3.2 判断 排序的字段值 sort 模型对象 if sort == 'price': sort_field = 'price' elif sort == 'hot': sort_field = '-sales' else: # 默认都是 创建时间排序 sort_field = 'create_time' # 3.3 根据排序规则 获取category_id 对应的所有 上架的商品 skus = SKU.objects.filter(category=category, is_launched=True).order_by(sort_field) # 4. 分页--django提供了分页器 # 4.1 实例化 分页器(分页的数据,每页显示的个数)==>返回每页显示的数据skus from django.core.paginator import Paginator paginator = Paginator(skus, 5) # 4.2 告诉分页器 当前显示的页数 try: page_skus = paginator.page(page_num) except Exception as e: return HttpResponseNotFound('当前页数超出范围了!') # 4.3 获取总页数 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)
def get(self, request, sku_id): try: sku = SKU.objects.get(id=sku_id) except Exception as e: return render(request, '404.html') # 面包屑组件 breadcrumb = get_breadcrumb(sku.category) # 商品分类三级菜单d categories = get_categories() # 获取当前商品的规格 sku_specs = sku.specs.order_by('spec_id') sku_key = [] for spec in sku_specs: sku_key.append(spec.option_id) # 1. 获取当前商品的所有 sku # skus = sku.spu.sku_set.all() # 2. 获取当前商品的所有 sku skus = SKU.objects.filter(spu=sku.spu) # 构建不同参数的sku字典 spec_sku_map = {} for s in skus: key = [] s_specs = s.specs.order_by('spec_id') for spec in s_specs: key.append(spec.option_id) spec_sku_map[tuple(key)] = s.id # 获取当前商品的规格 goods_specs = sku.spu.specs.order_by('id') for index, spec in enumerate(goods_specs): key = sku_key[:] spec_options = spec.options.all() for option in spec_options: key[index] = option.id option.sku_id = spec_sku_map.get(tuple(key)) spec.spec_options = spec_options # 获取商品评论 comments = OrderGoods.objects.filter(sku=sku).exclude(comment='') # 商品评论总数 count = len(comments) # 渲染页面 context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sku': sku, 'specs': goods_specs, 'count': count } return render(request, 'detail.html', context)
def get(self, request, category_id, page_num): # 判断category_id是否正确 try: category = GoodsCategory.objects.get(id=category_id) except GoodsCategory.DoesNotExist: return http.HttpResponseNotFound('GoodsCategory does not exist') # 调用商品频道三级联动 categories = get_categories() # 调用面包屑导航 breadcrumb = get_breadcrumb(category) # 排序 # 1 接收参数 sort = request.GET.get('sort', 'default') # 2 校验参数 if sort == 'price': sort_filed = 'price' elif sort == 'hot': sort_filed = '-sales' else: sort_filed = 'create_time' # 3 对商品信息进行排序 skus = SKU.objects.filter(category=category, is_launched=True).order_by(sort_filed) # 分页 # 1 创建分页器对象 paginator = Paginator(skus, constants.GOODS_LIST_LIMIT) # 2 获取每页的数据 try: page_skus = paginator.page(page_num) except EmptyPage: # 如果page_num不正确,默认给用户404 return http.HttpResponseNotFound('empty page') # 3 获取总页数 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)
def get(self, request, category_id, page_num): """提供商品列表页""" # 判断category_id是否正确 try: category = GoodsCategory.objects.get(pk=category_id) except GoodsCategory.DoesNotExist: return http.HttpResponseBadRequest('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 = 'default' sort_field = 'create_time' skus = SKU.objects.filter(category=category, is_launched=True).order_by(sort_field) # 创建分页器:每页N条记录 paginator = Paginator(skus, 5) # 获取每页商品数据 try: page_skus = paginator.page(page_num) except Exception as e: logger.error(e) # 如果page_num不正确,默认给用户404 return http.HttpResponseNotFound('empty page') # 获取列表页总页数 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=context)
def get(self, request, category_id, page_num): """ 1.判断category_id 2.获取分类数据 3.获取面包屑数据 4.组织排序字段 5.分页 6.组织数据 7.返回相应 """ # 1.判断category_id try: category = GoodsCategory.objects.get(pk=category_id) except Exception as e: return http.HttpResponseNotFound('分类数据错误') # 2.获取分类数据 categories = get_categories() # 3.获取面包屑数据 breadcrumb = get_breadcrumb(category) # 4.组织排序字段 sort = request.GET.get('sort') order_field = '' if sort == 'price': order_field = 'price' elif sort == 'hot': order_field = '-sales' else: sort = 'default' order_field = 'create_time' skus = SKU.objects.filter(category=category, is_launched=True).order_by(order_field) # 5.分页 paginator = Paginator(skus, per_page=5) try: page_num = int(page_num) page_skus = paginator.page(page_num) except Exception as e: return http.HttpResponseNotFound('empty page') #总页数 total_page = paginator.num_pages # 6.组织数据 context = { 'categories': categories, 'breadcrumb': breadcrumb, 'sort': sort, # 排序字段 'category': category, # 第三级分类 'page_skus': page_skus, # 分页后数据 'total_page': total_page, # 总页数 'page_num': page_num, # 当前页码 } # 7.返回相应 return render(request, 'list.html', context=context)
def get(self, request): """提供首页广告界面""" # 查询商品频道和分类 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=context)
def get(self, request): 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 get(self, request, category_id, page_num): """ :param category_id: 三级分类ID :param page_num: 当前页数 """ try: cat3 = GoodsCategory.objects.get(pk=category_id) except: return http.HttpResponseForbidden('商品不存在!') # 1.获取 商品 分类数据 categories = get_categories() # 2.面包屑组件的功能 breadcrumbs = get_breadcrumb(cat3) # 3. 排序 --获取 所有 SKU的值 # 前端传入的 sort 值 和 数据库的 字段 不对应 , 所以需要代码转换 sort = request.GET.get('sort') if sort == "price": order_field = 'price' elif sort == 'hot': order_field = '-sales' else: order_field = 'create_time' # 查询出来的 所有 skus skus = SKU.objects.filter(category=cat3, is_launched=True).order_by(order_field) # 1.导包分页器 from django.core.paginator import Paginator # 2.实例化 paginator = Paginator(skus, 5) # 3.获取 当前页的 skus数据 page_skus = paginator.page(page_num) # 4.获取总个数 total_page = paginator.num_pages context = { 'categories': categories, 'breadcrumbs': breadcrumbs, 'sort': sort, # 排序字段 'category': cat3, # 第三级分类 'page_skus': page_skus, # 分页后数据 'total_page': total_page, # 总页数 'page_num': page_num, # 当前页码 } return render(request, 'list.html', context)
def get(self, request, category_id, page_num): """提供商品列表页""" # 1.判断category_id是否存在 try: category = GoodsCategory.objects.get(id=category_id) except GoodsCategory.DoesNotExist: return HttpResponseBadRequest('category参数错误') # 2.查询商品频道分类,查询面包屑导航 categories = get_categories() breadcrumb = get_breadcrumb(category) # 3.根据sort方式 确认不同的排序字段,传递不同的数据进行数据排序 sort = request.GET.get('sort', 'default') if sort == 'price': # 价格 升序 sort_field = 'price' elif sort == 'hot': # 人气 按照销量降序 sort_field = '-sales' else: # 默认按照创建时间升序 sort_field = 'create_time' # 根据排序字段查出所有数据 datas_obj = SKU.objects.filter(category=category).order_by(sort_field) # 4.根据当前页号,每次返回5条 # ① 创建分页器 page_obj = Paginator(datas_obj, 5) try: # ② 获取指定页面的数据 datas = page_obj.page(page_num) except EmptyPage: # 如果page_num不正确,默认给用户404 return HttpResponseNotFound('empty page') # ③获取列表页总页数 total_page = page_obj.num_pages # 5.组装渲染数据并返回响应 # 渲染页面 context = { 'categories': categories, # 频道分类 'breadcrumb': breadcrumb, # 面包屑导航 'sort': sort, # 排序字段 'category': category, # 第三级分类 'page_skus': datas, # 分页后数据 'total_page': total_page, # 总页数 'page_num': page_num, # 当前页码 } return render(request, 'list.html', context)
def get(self, request, sku_id): try: sku = SKU.objects.get(id=sku_id) except SKU.DoesNotExist: 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) skus = sku.spu.sku_set.all() spec_sku_map = {} for s in skus: s_specs = s.specs.order_by('spec_id') key = [] for spec in s_specs: key.append(spec.option.id) spec_sku_map[tuple(key)] = s.id goods_specs = sku.spu.specs.order_by('id') if len(sku_key) < len(goods_specs): return for index, spec in enumerate(goods_specs): key = sku_key[:] spec_options = spec.options.all() for option in spec_options: 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)
def generate_static_index_html(): """ 生成静态的主页html文件 """ # <1> 获取首页需要的 数据库数据 # 1.获取三级分类商品 数据 categories = get_categories() # 2.获取广告数据 contents = {} # 2.1 获取所有的 广告分类 ad_categories = ContentCategory.objects.all() # 2.2 遍历分类 取出每个分类广告 for category in ad_categories: contents[category.key] = category.content_set.filter( status=True).order_by('sequence') context = { 'categories': categories, 'contents': contents, } # <2> 获取 index 模板文件 loader加载 # from django.template import loader template = loader.get_template('index.html') # <3> 渲染数据到模板文件 ==> html_text(渲染完毕的纯字符串) html_text = template.render(context) # <4> 将html_text 写入到本地文件 file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html') with open(file_path, 'w') as f: f.write(html_text) # 广告商没钱了 过节--重新生成首页--定时任务crontab静态化首页--dev.py # 验证 # python manage.py shell # >>> from apps.contents.crons import generate_static_index_html # >>> generate_static_index_html() # 开启测试静态服务器 # python -m http.server 8080 --bind 127.0.0.1
def get(self, request, category_id, page_num): # category_id 三级分类ID # 1.三级分类 调用 contents 封装好的代码 categories = get_categories() # 2.面包屑组件 cat3.parent # 获取cat3对象 cat3 = GoodsCategory.objects.get(id=category_id) bread_crumb = get_breadcrumb(cat3) # 3.排序 按照排序规则查询该分类商品SKU信息 # 3.1 sort == 'price':前端传入参数 hot default 不能直接对接数据库 sort = request.GET.get('sort') if sort == 'price': order_field = 'price' elif sort == 'hot': order_field = '-sales' else: order_field = 'create_time' # 3.2 SKU.objects.filter().order_by() skus = SKU.objects.filter(category=cat3, is_launched=True).order_by(order_field) print(skus) # 4.分页器--paginator from django.core.paginator import Paginator # 4.1 一页显示几个 paginator = Paginator(skus, contants.GOODS_LIST_LIMIT) # 4.2 总页数 all_pages = paginator.num_pages # 4.3 当前页 显示的内容 page_skus = paginator.page(page_num) context = { 'categories': categories, # 频道分类 'breadcrumb': bread_crumb, # 面包屑导航 'sort': sort, # 排序字段 'category': cat3, # 第三级分类 'page_skus': page_skus, # 分页后数据 'total_page': all_pages, # 总页数 'page_num': page_num, # 当前页码 } return render(request, 'list.html', context)
def get(self, request): # 1.获取三级分类商品 数据 categories = get_categories() # 2.获取广告数据 contents = {} # 2.1 获取所有的 广告分类 ad_categories = ContentCategory.objects.all() # 2.2 遍历分类 取出每个分类广告 for category in ad_categories: contents[category.key] = category.content_set.filter(status=True).order_by('sequence') context = { 'categories': categories, 'contents': contents, } return render(request, 'index.html', context)
def get(self, request, category_id, page_num): try: category = GoodsCategory.objects.get(id=category_id) except Exception as e: return http.HttpResponseNotFound('GoodsCategory does not exist') breadcrumb = get_breadcrumb(category) categories = get_categories() sort = request.GET.get('sort', 'default') if sort == 'price': sort_field = 'price' elif sort == 'hot': sort_field = '-sales' else: sort_field = 'create_time' skus = SKU.objects.filter(category=category, is_launched=True).order_by(sort_field) paginator = Paginator(skus, 5) # 总页数 total_page = paginator.num_pages # 每一页的商品 try: page_skus = paginator.page(page_num) except Exception as e: return http.HttpResponseNotFound('empty page') context = { 'categories': categories, 'breadcrumb': breadcrumb, 'category': category, 'sort': sort, 'page_num': page_num, 'total_page': total_page, 'page_skus': page_skus } return render(request, 'list.html', context)
def get(self,request): # 1.分类信息 # 分类信息在其他页面也会出现,我们应该直接抽取为一个方法 # 查询商品频道和分类 categories = get_categories() # 2.楼层信息 # 广告数据 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=context)
def get(self, request, category_id, page_num): # category_id 三级分类id category = GoodsCategory.objects.get(id=category_id) # 1. 商品分类数据 categories = get_categories() # 2.获取面包屑组件 bread_crumb = get_breadcrumb(category) # 3. 排序判断 default==create_time price=price hot==-sales sort = request.GET.get('sort') order_field = "create_time" if sort == "price": order_field = "price" elif sort == "hot": order_field = "-sales" else: order_field = "create_time" # 获取 所有上架的商品(对应的类)---排序 skus = SKU.objects.filter(category=category, is_launched=True).order_by(order_field) # 每页有几个; 每页显示哪几个 ; 总页数; 当前页数 page_num from django.core.paginator import Paginator paginator = Paginator(skus, 5) page_skus = paginator.page(page_num) total_nums = paginator.num_pages context = { 'categories': categories, 'breadcrumb': bread_crumb, 'category': category, 'sort': sort, # 排序字段 'page_skus': page_skus, # 分页后数据 'total_page': total_nums, # 总页数 'page_num': page_num, # 当前页码 } return render(request, 'list.html', context)