Django Celery发送注册邮箱不起作用

时间:2013-12-29 15:26:57

标签: python django celery

我正在学习芹菜。在我的网站上,我让人们注册一个帐户。创建新帐户后,它会自动向其用户电子邮件地址发送激活电子邮件。

一切正常但现在我想使用Celery异步发送电子邮件。

我使用RabbitMQ作为代理,版本3.1.5和Celery 3.1.7(最新版本),因为他们说这个版本不需要djcelery。所以我只需要安装Celery。

我按照celery的说明在其网站上配置了我的django。

proj
  --proj/celery.py

这是我的 celery.py

from __future__ import absolute_import

import os

from celery import Celery

from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print('Request:{0!r}'.format(self.request))

这是我放在app文件夹中的 tasks.py

proj
 ---proj/celery.py
 ---app/tasks.py

tasks.py

from __future__ import absolute_import

from celery.decorators import task

@task()
def user_send_activation_email(user):
    user.send_activation_email()

我将send_activation_email功能添加到用户模型中,因此用户可以发送电子邮件。

这是我的 views.py

from django.shortcuts import render_to_response, redirect,render
from django.conf import settings
from django.http import HttpResponse,HttpResponseRedirect,Http404
from django.template import RequestContext
from app.accounts.forms import SignupForm
from django.contrib.auth import get_user_model
from app.sns_utils import generate_sha1 
from app.tasks import user_send_activation_email 

def signup(request):
    if request.method=='POST':
        form=SignupForm(data=request.POST)
        if form.is_valid():
            UserModel=get_user_model()
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
             user=UserModel.objects.create_user(username=username,email=email,password=password)
            user.activation_key = generate_sha1(username)
            user.save()
            user_send_activation_email.delay(user=user)


            return redirect('/')

以下是我在 settings.py 中配置Celery的方法:

BROKER_URL = 'amqp://guest:guest@localhost:5672//'

CELERY_RESULT_BACKEND = 'amqp://guest:guest@localhost:5672//'

CELERY_TASK_SERIALIZER = 'json'

一切都只是跟芹菜网站说的那样。

manage.py runserver后,在注册页面中,我使用用户名vmuser630注册了一个新帐户,并显示以下错误:

EncodeError at /accounts/signup/
<MyUser: vmuser630> is not JSON serializable
Request Method: POST
Request URL:    http://localhost:8000/accounts/signup/
Django Version: 1.6
Exception Type: EncodeError
Exception Value:    
<MyUser: vmuser630> is not JSON serializable
Exception Location: C:\Python33\lib\json\encoder.py in default, line 173
Python Executable:  C:\Python33\python.exe
Python Version: 3.3.2

如果我从 settings.py 中删除此内容:

CELERY_TASK_SERIALIZER = 'json'

再试一次manage.py runserver,我只是重定向到'/'页面。新帐户在数据库中,但我没有收到激活电子邮件。

看起来发送电子邮件任务刚刚通过,没有任何反应。

当我将发送电子邮件功能更改回:

user.send_activation_email()

这意味着我不使用芹菜,它有效,我可以获得激活电子邮件。

为什么它显示错误不是JSON可序列化

3 个答案:

答案 0 :(得分:18)

Celery需要序列化任务的参数。在这里,user不可序列化,但您可以将其替换为ID:

tasks.py

from __future__ import absolute_import

from celery.decorators import task
from django.contrib.auth import get_user_model

@task()
def user_send_activation_email(user_id):
    user = get_user_model().objects.get(pk=user_id)
    user.send_activation_email()

然后使用

views.py 中调用它
user_send_activation_email.delay(user_id=user.pk)

答案 1 :(得分:6)

请参阅setting-task_serializer,您可以在celery conf中设置CELERY_TASK_SERIALIZER ='pickle'。

答案 2 :(得分:-1)

Celery只能serialize to JSON个字符串,Unicode,浮点数,布尔值,字典和列表。看起来它正在尝试并且无法序列化MyUser对象。

您可能有兴趣使用django-post_office代替异步电子邮件。