0

I have a blog and I want to send e-mail my subscribes when I published new post. I used django signals. My problem is that signal is not working. I can send mail when I try django shell.

This is my signals.py file :

from django.db.models.signals import post_save
from blog.models import Blog
from django.core.mail import send_mail
from django.dispatch import receiver

@receiver(post_save, sender=Blog)
def new_post_created(sender,**kwargs)
    print "Success"
    mysubject = "E mail gönderiyorum"
    mymessage = "Merhaba, \nYeni yazım az önce yayınlandı. Hemen okumak için linki kullanabilirsin.\n http://www.cemreacar.com/blog/"
    mymail = "mail@cemreacar.com"
    send_mail(mysubject,mymessage,mymail,['*****@gmail.com'],'fail_silently=False')
cmracar
  • 19
  • 7
  • Does your console print "Success"? – Hybrid May 02 '20 at 16:11
  • have you fill apps.py and import your signals file in AppModel like in that post? https://stackoverflow.com/a/56134874/8548036 – ncopiy May 02 '20 at 16:18
  • @ncopiy When I try , settings.py throw error. – cmracar May 02 '20 at 16:27
  • @cmracar which error? show the trace – ncopiy May 02 '20 at 16:28
  • @ncopiy Exception in thread django-main-thread: ... apps.populate(settings.INSTALLED_APPS) File "/home/ubuntu/env/lib/python3.8/site-packages/django/apps/registry.py", line 93, in populate raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: blog – cmracar May 02 '20 at 16:31
  • @cmracar okey, now please show me full apps.py file in application where your signal receiver lives – ncopiy May 02 '20 at 16:33
  • @Hybrid No , there is no success message. – cmracar May 02 '20 at 16:35
  • @ncopiy from django.apps import AppConfig class BlogConfig(AppConfig): name = 'blog' def ready(self): import blog.signals – cmracar May 02 '20 at 16:37
  • @cmracar i guess somewhere in your apps there is another `AppConfig` with name 'blog'. try to use another one name (with settings.INSTALLED_APPS fix too) – ncopiy May 02 '20 at 16:43
  • @ncopiy Should I add like this ; class EmailBlogConfig(AppConfig): name = 'blog' def ready(self): import blog.signals – cmracar May 02 '20 at 16:56
  • @cmracar yes but check: name field must contains string with current application name. If your apps.py path is: `email_blog/apps.py` i means your `EmailBlogConfig` `name` field must be `"emails_blog"` – ncopiy May 02 '20 at 17:02
  • @ncopiy but , in this state , I will need to create new app , I do not want to this. why this is so confused? – cmracar May 02 '20 at 17:39

1 Answers1

0

Have you overridden ready method? If not you have to override ready method of application configuration class.

In apps.py application configuration class will be defined. Override ready method as follows

from django.apps import AppConfig
class YourAppConfig(AppConfig):
    name = 'your_app'

    def ready(self):
        import your_app.signal

In project settings.py update installed apps as follows:

Replace

INSTALLED_APPS = (
    '...',
    'your_app',
)

with

INSTALLED_APPS = (
    '...',
    'your_app.apps.YourAppConfig',
)

Documentation link - https://docs.djangoproject.com/en/3.0/ref/applications/#django.apps.AppConfig.ready

ABN
  • 1,024
  • 13
  • 26