商品详情
方法一:使用脚本生成
1.在项目根目录下创建 script 包目录 ,然后在创建的包目录下创建 test.py 文件,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
import sys sys.path.insert(0, '../')
import os if not os.getenv('DJANGO_SETTINGS_MODULE'): os.environ['DJANGO_SETTINGS_MODULE'] = 'haoke_mall.settings.dev'
import django django.setup()
from django.template import loader from django.conf import settings
from goods.utils import get_categories from goods.models import SKU
def generate_static_sku_detail_html(sku_id): """ 生成静态商品详情页面 :param sku_id: 商品sku id """ categories = get_categories()
sku = SKU.objects.get(id=sku_id) sku.images = sku.skuimage_set.all()
goods = sku.goods goods.channel = goods.category1.goodschannel_set.all()[0]
sku_specs = sku.skuspecification_set.order_by('spec_id') sku_key = [] for spec in sku_specs: sku_key.append(spec.option.id)
skus = goods.sku_set.all()
spec_sku_map = {} for s in skus: s_specs = s.skuspecification_set.order_by('spec_id') key = [] for spec in s_specs: key.append(spec.option.id) spec_sku_map[tuple(key)] = s.id
specs = goods.goodsspecification_set.order_by('id') if len(sku_key) < len(specs): return for index, spec in enumerate(specs): key = sku_key[:] options = spec.specificationoption_set.all() for option in options: key[index] = option.id option.sku_id = spec_sku_map.get(tuple(key))
spec.options = options
context = { 'categories': categories, 'goods': goods, 'specs': specs, 'sku': sku }
template = loader.get_template('detail.html') html_text = template.render(context) file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'goods/'+str(sku_id)+'.html') with open(file_path, 'w') as f: f.write(html_text)
if __name__ == '__main__': skus = SKU.objects.all() for sku in skus: print(sku.id) generate_static_sku_detail_html(sku.id)
|