简体   繁体   中英

Django-celery is not working

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

Trackback :- *

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

I don't know what is wrong with my this simple code. This is not working and it made me really disparate. I am working on this from 4 days. I am new to python,django and celery. Is there any one who will help me on this ? Please provide me full instruction as i am new to python.

In your views.py you try to import from a module named .tasks

from .tasks import hello_world

based on your stack trace this doesn't exist

Exception Value: No module named tasks

are you sure you have a module called tasks in that level of you import structure?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM