Here are the examples of the python api django.template taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
0
View Complete Implementation : tasks.py
Copyright BSD 2-Clause "Simplified" License
Author : ScrappyZhang
Copyright BSD 2-Clause "Simplified" License
Author : ScrappyZhang
@app.task
def generate_static_index_html():
"""使用celery生成静态首页文件"""
# 获取商品的分类信息
types = GoodsType.objects.all()
# 获取首页的轮播商品的信息
index_banner = IndexGoodsBanner.objects.all().order_by('index')
# 获取首页的促销活动的信息
promotion_banner = IndexPromotionBanner.objects.all().order_by('index')
# 获取首页分类商品的展示信息
for category in types:
# 获取type种类在首页展示的图片商品的信息和文字商品的信息
image_banner = IndexTypeGoodsBanner.objects.filter(category=category, display_type=1)
satle_banner = IndexTypeGoodsBanner.objects.filter(category=category, display_type=0)
# 给category对象增加属性satle_banner,image_banner
# 分别保存category种类在首页展示的文字商品和图片商品的信息
category.satle_banner = satle_banner
category.image_banner = image_banner
cart_count = 0
# 组织模板上下文
context = {
'types': types,
'index_banner': index_banner,
'promotion_banner': promotion_banner,
'cart_count': cart_count,
}
# 使用模板
# 1.加载模板文件
from django.template import loader
temp = loader.get_template('static_index.html')
# 2.模板渲染
static_html = temp.render(context)
# 3.生成静态首页文件
save_path = os.path.join(settings.BASE_DIR, 'static/index.html')
with open(save_path, 'w') as f:
f.write(static_html)