celery.py: -Django的芹菜不工作
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和芹菜的新手。有沒有人會幫助我呢?請爲我提供完整的指導,因爲我是python的新手。
請顯示您的應用程序的文件結構。視圖和任務在同一目錄中嗎? – 2014-10-10 20:12:47
'tasks.py'和'views.py'在同一個文件夾中嗎? – Zulu 2014-10-10 20:45:47
no tasks.py和views.py不在同一個文件夾中 – 2014-10-11 08:21:07