简体   繁体   English

在 Django 中创建新帖子时自动发送电子邮件

[英]Auto send email when new post has created in django

When I save my post, I want to send email for user automatically.当我保存我的帖子时,我想自动为用户发送电子邮件。 But it's not working.但它不起作用。 This is my code这是我的代码

signals.py信号.py

from django.db.models.signals import post_save
from django.dispatch import receiver
from mysite.models import Post, Email
from django.core.mail import send_mail
from blog.settings import EMAIL_HOST_USER

@receiver(post_save, sender=Post)
def SendEmail(sender , instance, created, **kwargs):
    if created:
        emails = list(Email.objects.values('email'))
        recepients = []
        for i in range(0, len(emails)):
            recepients.append(emails[i]['email'])
            pass
        send_mail('New post on blog', str(instance.title), EMAIL_HOST_USER, recepients, fail_silently=False)
        pass

If the signal itself 's not working, is because of this:如果信号本身不起作用,是因为:
By default signals.py doesn't get automatically loaded into your app, so you have to manually import it.默认情况下, signals.py不会自动加载到您的应用程序中,因此您必须手动导入它。
Assuming your app is named post , you need to modify your app's config file ( post/apps.py ) accordingly.假设您的应用名为post ,您需要相应地修改应用的配置文件 ( post/apps.py )。

This is an example how:这是一个例子:

# post/apps.py #
from django.apps import AppConfig

class PostConfig(AppConfig):
    name = 'post'    
    def ready(self):
        print(">DEBUG::loading_signals")
        import post.signals
        # other stuff you need imported on app's startup

If you're still not receiving signals, try specifying your app's default config class inside your app's __init__.py file, like so:如果您仍未收到信号,请尝试在应用的__init__.py文件中指定应用的默认配置类,如下所示:

# post/__init__.py #
default_app_config = "post.apps.PostConfig"

from Django docs itself:来自 Django 文档本身:

Where should this code [signals] live?这个代码[信号]应该在哪里?

Strictly speaking, signal handling and registration code can live anywhere you like, although it's recommended to avoid the application's root module and its models module to minimize side-effects of importing code.严格来说,信号处理和注册代码可以放在你喜欢的任何地方,尽管建议避免应用程序的根模块及其模型模块,以尽量减少导入代码的副作用。

In practice, signal handlers are usually defined in a signals submodule of the application they relate to.在实践中,信号处理程序通常定义在与它们相关的应用程序的信号子模块中。 Signal receivers are connected in the ready() method of your application configuration class.信号接收器在应用程序配置类的ready()方法中连接。 If you're using the receiver() decorator, import the signals submodule inside ready() .如果您使用receiver()装饰器,请在ready()导入信号子模块。

References:参考:
https://docs.djangoproject.com/en/3.0/ref/applications/#django.apps.AppConfig.ready https://docs.djangoproject.com/en/3.0/ref/applications/#django.apps.AppConfig.ready
https://docs.djangoproject.com/en/3.0/topics/signals/#connecting-receiver-functions https://docs.djangoproject.com/en/3.0/topics/signals/#connecting-receiver-functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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