其实挺简单的问题,但花了自己一个下午来解决,先是浏览各种博客,无果;没办法,然后去看celery官方文档,无果,近乎绝望,最后仔细看代码,找到问题所在(如下),自学狗这效率。。。。。。

下面是自己task.py中的代码

  1. # 使用celery
  2. from django.conf import settings
  3. from celery import Celery
  4. from django.template import loader, RequestContext
  5. from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner
  6. import os
  7.  
  8. # 在任务处理者一端加这几句
  9. import os
  10. import django
  11. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dailyfresh.settings")
  12. django.setup()
  13.  
  14. # 创建一个Celery类的实例对象
  15. app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/1')
  16.  
  17. # 定义任务函数
  18. @app.task
  19. def generate_static_index_html():
  20. """产生首页静态页面"""
  21. # 获取商品的种类信息
  22. types = GoodsType.objects.all()
  23.  
  24. # 获取首页轮播商品信息
  25. goods_banners = IndexGoodsBanner.objects.all().order_by('index')
  26.  
  27. # 获取首页促销活动信息
  28. promotion_banners = IndexPromotionBanner.objects.all().order_by('index')
  29.  
  30. # 获取首页分类商品展示信息
  31. for type in types: # GoodsType
  32. # 获取type种类首页分类商品的图片展示信息
  33. image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
  34. # 获取type种类首页分类商品的文字展示信息
  35. title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')
  36.  
  37. # 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
  38. type.image_banners = image_banners
  39. type.title_banners = title_banners
  40.  
  41. # 组织模板上下文
  42. context = {'types': types,
  43. 'goods_banners': goods_banners,
  44. 'promotion_banners': promotion_banners}
  45.  
  46. # 使用模板
  47. # 1.加载模板文件,返回模板对象
  48. temp = loader.get_template('static_index.html')
  49. # 2.模板渲染
  50. static_index_html = temp.render(context)
  51.  
  52. # 生成首页对应静态文件
  53. save_path = os.path.join(settings.BASE_DIR, 'static/index.html')
  54. with open(save_path, 'w') as f:
  55. f.write(static_index_html)

当使用celery -A celery_tasks.tasks worker -l info开启worker时,出现标题所示的报错,

错误原因:

  1. from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner

这行代码应该写在环境配置后面,不然python解释器找不到goods模块,具体代码如下

  1. # 使用celery
  2. from django.conf import settings
  3. from celery import Celery
  4. from django.template import loader, RequestContext
  5.  
  6. # 在任务处理者一端加这几句
  7. import os
  8. import django
  9. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dailyfresh.settings")
  10. django.setup()
  11.  
  12. from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner
  13.  
  14. # 创建一个Celery类的实例对象
  15. app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/1')
  16.  
  17. # 定义任务函数
  18. @app.task
  19. def generate_static_index_html():
  20. """产生首页静态页面"""
  21. # 获取商品的种类信息
  22. types = GoodsType.objects.all()
  23.  
  24. # 获取首页轮播商品信息
  25. goods_banners = IndexGoodsBanner.objects.all().order_by('index')
  26.  
  27. # 获取首页促销活动信息
  28. promotion_banners = IndexPromotionBanner.objects.all().order_by('index')
  29.  
  30. # 获取首页分类商品展示信息
  31. for type in types: # GoodsType
  32. # 获取type种类首页分类商品的图片展示信息
  33. image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
  34. # 获取type种类首页分类商品的文字展示信息
  35. title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')
  36.  
  37. # 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
  38. type.image_banners = image_banners
  39. type.title_banners = title_banners
  40.  
  41. # 组织模板上下文
  42. context = {'types': types,
  43. 'goods_banners': goods_banners,
  44. 'promotion_banners': promotion_banners}
  45.  
  46. # 使用模板
  47. # 1.加载模板文件,返回模板对象
  48. temp = loader.get_template('static_index.html')
  49. # 2.模板渲染
  50. static_index_html = temp.render(context)
  51.  
  52. # 生成首页对应静态文件
  53. save_path = os.path.join(settings.BASE_DIR, 'static/index.html')
  54. with open(save_path, 'w') as f:
  55. f.write(static_index_html)

 此时使用celery -A celery_tasks.tasks worker -l info 就能正常开启worker了

celery开启worker报错django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE o的更多相关文章

  1. django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call

    Error info: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, ...

  2. django调用py报错 django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured.

    完整报错信息如下 django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, bu ...

  3. 关于Django报错django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configure

    报错代码:django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but se ...

  4. 解决:django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not 的方法

    错误类型: 该错误是在在创建Django工程时出现遇到的错误 完整报错信息:(博文标题输入长度有限制) django.core.exceptions.ImproperlyConfigured: Req ...

  5. pythoncharm 中解决启动server时出现 “django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured”的错误

    背景介绍 最近,尝试着用pythoncharm 这个All-star IDE来搞一搞Django,于是乎,下载专业版,PJ等等一系列操作之后,终于得偿所愿.可以开工了. 错误 在园子里找了一篇初学者的 ...

  6. python基于Django框架编译报错“django.core.exceptions.ImproperlyConfigured”的解决办法?

    下面是我具体遇到的问题和解决方法: 错误详细信息: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_IND ...

  7. django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)

    在python的开发中,遇到了这个错误: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TA ...

  8. Django2.2报错 django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

    准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: django.core.exceptions.Improperly ...

  9. Django生成脚本迁移文件时,报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

    一.本人环境:django:3.0.2, python:3.8.1,  pymysql:0.9.3 二.解决步骤: 1.django目录下找到 base.py文件: 2.在base.py文件中注释以下 ...

随机推荐

  1. cf 11D A Simple Task(状压DP)

    题意: N个点构成的无向图,M条边描述这个无向图. 问这个无向图中共有多少个环. (1 ≤ n ≤ 19, 0 ≤ m) 思路: 例子: 4 6 1 2 1 3 1 4 2 3 2 4 3 4 答案: ...

  2. HTML基础强化

    1.如何理解HTML? HTML类似于一份word"文档" 描述文档的"结构" 有区块和大纲 2.对WEB标准的理解? Web标准是由一系列标准组合而成.一个网 ...

  3. GoLang设计模式12 - 空对象模式

    空对象设计模式是一种行为型设计模式,主要用于应对空对象的检查.使用这种设计模式可以避免对空对象进行检查.也就是说,在这种模式下,使用空对象不会造成异常. 空对象模式的组件包括: Entity:接口,定 ...

  4. Java测试开发--Set、Map、List三种集合(四)

    1.集合类型主要有3种:set(集).list(列表)和map(映射). 2.三者关系 3.Set set接口是Collection接口的一个子接口,是无序的,set去重,也就是说set中不存在两个这 ...

  5. css语法规范、选择器、字体、文本

    css语法规范 使用 HTML 时需要遵从一定的规范,CSS 也是如此.要想熟练地使用 CSS 对网页进行修饰,首先需要了解CSS 样式规则. CSS 规则由两个主要的部分构成:选择器以及一条或多条声 ...

  6. sqlalchemy insert on duplicate update

    sqlalchemy insert on duplicate update from sqlalchemy.dialects.mysql import insert insert_stmt = ins ...

  7. 华为C/C++编码规范+《数学之美》感想

    1.排版 1.1 程序块要采用缩进风格编写, 缩进的空格数为4个.(说明: 对于由开发工具自动生成的代码可以有不一致)1.2 相对独立的程序块之间.变量说明之后必须加空行.1.3 循环.判断等语句中若 ...

  8. ES6-正则新增(复习+学习)

    ES6-正则 昨天,复习了正则的基本知识,今天学习ES6新增的正则的知识,做一个总结笔记,大家可以先看4,5对应的方法然后再从头看,话不多说直接上: 1.RegExp构造函数的区别 2.新增的修饰符 ...

  9. 使用PAM模块实现普通用户之间su免密切换

    参考自:Allow user1 to "su - user2" without password https://unix.stackexchange.com/questions/ ...

  10. [cf1137F]Matches Are Not a Child's Pla

    显然compare操作可以通过两次when操作实现,以下仅考虑前两种操作 为了方便,将优先级最高的节点作为根,显然根最后才会被删除 接下来,不断找到剩下的节点中(包括根)优先级最高的节点,将其到其所在 ...