首先在启动celery时并没有出现问题,启动如上图,生成静态页面代码如下图
@app.task
def generate_static_index_html():
"""产生首页静态页面"""
# 获取商品的种类信息
types = GoodsType.objects.all()
# 获取首页轮播商品信息
goods_banners = IndexGoodsBanner.objects.all().order_by('index')
# 获取首页促销活动信息
promotion_banners = IndexPromotionBanner.objects.all().order_by('index')
# 获取首页分类商品展示信息
for typ in types: # GoodsType
# 获取type种类首页分类商品的图片展示信息
image_banners = IndexTypeGoodsBanner.objects.filter(type=typ, display_type=1).order_by('index')
# 获取type种类首页分类商品的文字展示信息
title_banners = IndexTypeGoodsBanner.objects.filter(type=typ, display_type=0).order_by('index')
# 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
typ.image_banners = image_banners
typ.title_banners = title_banners
# 组织模板上下文
context = {'types': types,
'goods_banners': goods_banners,
'promotion_banners': promotion_banners}
# 使用模板
# 1.加载模板文件,返回模板对象
temp = loader.get_template('static_index.html')
# 2.模板渲染
static_index_html = temp.render(context)
# 生成首页对应静态文件
save_path = os.path.join(settings.BASE_DIR, 'static/index.html')
with open(save_path, 'w', encoding='utf-8') as f:
f.write(static_index_html)
使用django shell 调试之后 错误就来了
错误如下
这个问题困扰了我一天没有任何解决办法,各种各样奇奇怪怪的解决方法都试过了网上也没用找到解决方案,然后我突然怀疑是不是版本的问题,毕竟现在django已经到了4.0版本,我这个项目还用的是2.2版本的django,在安装celery时也没有多想,毕竟前一个异步发送邮件是正常运行了的,抱着最后一丝希望我怀疑是celery的版本太新了,于是我卸载掉了原有的celery是5.3版本的,装回了兼容django2.2的5.0.5版本,然后重启celery终于tmd解决了(学习celery真的是折磨),所以我想着这个问题无论如何也要分享出来
先卸载掉原有的celery
指定安装5.0.5版本的celery
然后重启问题得到解决
如果重启的时候遇到了
执行这样几步
然后安装celery common就行了
再次生成静态页面,终于没有报错了T-T~~