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

 愤然尔立_980 发布于 2023-02-07 16:42

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

一切都运作良好,但现在我想使用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/
 is not JSON serializable
Request Method: POST
Request URL:    http://localhost:8000/accounts/signup/
Django Version: 1.6
Exception Type: EncodeError
Exception Value:    
 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可序列化

2 个回答
  • 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)
    

    2023-02-07 16:44 回答
  • 请参阅setting-task_serializer,您可以在芹菜conf中设置CELERY_TASK_SERIALIZER ='pickle'.

    2023-02-07 16:44 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有