【发布时间】:2014-12-06 00:41:52
【问题描述】:
celery.py :-
from future import absolute_import
import os
import django
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')
django.setup()
app = Celery('demo')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
tasks.py:-
from demo.celery import app
@app.task
def hello_world():
print('Hello World')
views.py:-
from django.shortcuts import render
from django.views.generic import TemplateView
from .tasks import hello_world
class IndexView(TemplateView):
template_name = 'home/index.html'
def get_context_data(self, kwargs):
context = super(IndexView, self).get_context_data(kwargs)
hello_world.delay()
return context
引用:- *
Environment:
Request Method: GET
Request URL: http://localhost:8000/hello/
Django Version: 1.7
Python Version: 2.7.7
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djcelery')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
98. resolver_match = resolver.resolve(request.path_info)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
340. sub_match = pattern.resolve(new_path)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
224. return ResolverMatch(self.callback, args, kwargs, self.name)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in callback
231. self._callback = get_callable(self._callback_str)
File "C:\Python27\lib\site-packages\django\utils\lru_cache.py" in wrapper
101. result = user_function(*args, **kwds)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in get_callable
97. mod = import_module(mod_name)
File "C:\Python27\lib\importlib\__init__.py" in import_module
37. __import__(name)
File "C:\app1\app\home\views.py" in <module>
4. from .tasks import hello_world
Exception Type: ImportError at /hello/
Exception Value: No module named tasks
我不知道我这个简单的代码有什么问题。这是行不通的,它让我真的与众不同。我从4天开始工作。我是 python、django 和 celery 的新手。有没有人会在这方面帮助我?请给我完整的指导,因为我是 python 新手。
【问题讨论】:
-
请显示您应用的文件结构。视图和任务是否在同一个目录中?
-
tasks.py和views.py在同一个文件夹中吗? -
没有 tasks.py 和 views.py 不在同一个文件夹中
标签: python django python-2.7 celery