其實挺簡單的問題,但花了自己一個下午來解決,先是瀏覽各種博客,無果;沒辦法,然后去看celery官方文檔,無果,近乎絕望,最后仔細看代碼,找到問題所在(如下),自學狗這效率。。。。。。
下面是自己task.py中的代碼
# 使用celery from django.conf import settings from celery import Celery from django.template import loader, RequestContext from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner import os # 在任務處理者一端加這幾句 import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dailyfresh.settings") django.setup() # 創建一個Celery類的實例對象 app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/1') # 定義任務函數 @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 type in types: # GoodsType # 獲取type種類首頁分類商品的圖片展示信息 image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index') # 獲取type種類首頁分類商品的文字展示信息 title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index') # 動態給type增加屬性,分別保存首頁分類商品的圖片展示信息和文字展示信息 type.image_banners = image_banners type.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') as f: f.write(static_index_html)
當使用celery -A celery_tasks.tasks worker -l info開啟worker時,出現標題所示的報錯,
錯誤原因:
from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner
這行代碼應該寫在環境配置后面,不然python解釋器找不到goods模塊,具體代碼如下
# 使用celery from django.conf import settings from celery import Celery from django.template import loader, RequestContext # 在任務處理者一端加這幾句 import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dailyfresh.settings") django.setup() from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner # 創建一個Celery類的實例對象 app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/1') # 定義任務函數 @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 type in types: # GoodsType # 獲取type種類首頁分類商品的圖片展示信息 image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index') # 獲取type種類首頁分類商品的文字展示信息 title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index') # 動態給type增加屬性,分別保存首頁分類商品的圖片展示信息和文字展示信息 type.image_banners = image_banners type.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') as f: f.write(static_index_html)
此時使用celery -A celery_tasks.tasks worker -l info 就能正常開啟worker了