1

我正在尝试使用 django-notification 发送电子邮件。我已经按照文档创建了所有通知类型、模板和 management.py。但是,当我尝试发送电子邮件时,显示此调试页面并显示错误:NoticeType 匹配查询不存在。这是一些代码。

在我的 management.py 文件中:

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"

在我的 settings.py 文件中:

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 = 'texturamail@gmail.com'
EMAIL_HOST_PASSWORD = #not shown
EMAIL_PORT = 587
NOTIFICATION_BACKENDS = [("texturamail@gmail.com", "notification.backends.email.EmailBackend"),]
#other code....

在我的 models.py 文件中:

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})

抱歉,页面代码超载。也就是说,我做错了什么吗?有什么遗漏吗?任何见解将不胜感激!

4

2 回答 2

0

发生这种情况是因为您尝试将不存在的东西分配给NoticeType对象。问题是我真的不明白代码的哪一部分是这样做的,但是添加try:except:可以解决问题。每当您匹配查询以添加它时尝试一下,您会找到它。

于 2013-03-21T20:40:47.890 回答
0

发生这种情况是因为您NoticeType使用标签创建了create_model, delete_model, edit_model。但是当您notifications.send使用不同的标签发送通知时:create_email等标签必须与您首先创建的标签相同。您应该更改create_mailcreate_model notification.send([user], "create_model", {'user':user, 'first':first_name, 'last':last_name}).

于 2016-10-05T17:34:28.467 回答