I’m trying to send email using django-notification. I have followed the documentation and created all the notice types, templates and management.py. However, when I attempt to send an email, I’m shown this debug page with the error: NoticeType matching query does not exist. Here’s some of the code.
In my management.py file:
from django.conf import settings
from django.db.models import signals
from django.utils.translation import ugettext_noop as _
if "notification" in settings.INSTALLED_APPS:
from notification import models as notification
def create_notice_types(app, created_models, verbosity, **kwargs):
notification.NoticeType.create("create_model", _("Model Creation"), _("An entry has been created"))
notification.NoticeType.create("delete_model", _("Model Deletion"), _("An entry has been deleted"))
notification.NoticeType.create("edit_model", _("Model Change"), _("An entry has been changed."))
signals.post_syncdb.connect(create_notice_types, sender=notification)
else:
print "Skipping creation of NoticeTypes as notification app not found"
In my settings.py file:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'departments',
'notification'
)
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = #not shown
EMAIL_PORT = 587
NOTIFICATION_BACKENDS = [("[email protected]", "notification.backends.email.EmailBackend"),]
#other code....
In my models.py file:
def model_create_edit(sender, **kwargs):
instance = kwargs.get('instance')
user, first_name, last_name = instance.user, instance.user.first_name, instance.user.last_name
if kwargs['created']:
notification.send([user], "create_email", {'user':user, 'first':first_name, 'last':last_name})
else:
notification.send([user], "edit_email", {'user':user, 'first':first_name, last':last_name})
def model_deletion(sender, **kwargs):
instance = kwargs.get('instance')
user, first_name, last_name = instance.user, instance.user.first_name, instance.user.last_name
notification.send([user], "delete_email", {'user':user, 'first':first_name, 'last':last_name})
Sorry for overloading the page with code. That said, is there something I’m doing wrong? Anything missing? Any insight would be greatly appreciated!
This is happening because you have created NoticeType
with label create_model
, delete_model
, edit_model
. but when you are sending notifications using notifications.send
you are using different label: create_email
etc. labels must be same that you created first.
you should change create_mail
to create_model
notification.send([user], "create_model", {'user':user, 'first':first_name, 'last':last_name})
.
This is happening because you try to assign something that doesn’t exist to the NoticeType
object. The thing is that I don’t really understand which part of the code does that, but adding try:
, except:
would solve the problem. Try whenever you are matching queries to add it and you will locate it.