Django Celery发送注册电子邮件不起作用 [英] Django Celery send register email do not work

查看:144
本文介绍了Django Celery发送注册电子邮件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am learning Celery. In my website, I let people register an account. Once they create a new account, it will automatically send an activation email to their user email address.

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

Everything works well but now I want to use Celery to send the email asynchronously.

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

I use RabbitMQ as broker, version 3.1.5 and Celery 3.1.7 (the latest version), as they say this version does not need djcelery. So all I need is just to install Celery.

我按照celery网站上的说明进行操作,配置Django。

I followed the instruction as celery its website, Configurate my 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))

这是我放入应用程序文件夹中的 tasks.py

And this is the tasks.py that I put in my app folder:

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 函数,以便用户可以发送电子邮件。

I added the send_activation_email function to user models, so user can send the 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的方式:

And here is how I configured Celery in settings.py:

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

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

CELERY_TASK_SERIALIZER = 'json'

一切都遵循celery网站上的说法。

everything just follow celery its website say.

一旦我 manage.py runserver ,在注册页面中,我用用户名<$ c注册了一个新帐户。 $ c> vmuser630 ,它显示此错误:

Once I manage.py runserver, in the signup page, I register a new account with username vmuser630, and it shows this error:

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 ,我被重定向到 /页面。新帐户位于数据库中,但是我没有收到激活电子邮件。

and try again manage.py runserver, I'm just redirected to the '/' page. The new account is in the database, but I did not get the activation email.

看起来发送电子邮件任务刚刚通过,什么也没发生。

It looks like the send email task has just been passed, nothing happened.

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

When i change the send email function back to:

user.send_activation_email()

这表示我不使用芹菜,它可以正常工作,我可以获得激活电子邮件。

it means I do not user celery, it works, I can get the activation email.

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

推荐答案

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

Celery need to serialize the arguments for a task. Here, user is not serializable, but you can replace it with its ID:

任务中。 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

Then call it from views.py using:

user_send_activation_email.delay(user_id=user.pk)

这篇关于Django Celery发送注册电子邮件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆