我想在创建新帖子时向“观察者”多对多字段中定义的用户列表发送通知电子邮件。
帖子创建没有错误,并且观察者用户列表成功添加到其中(它们出现在 post_detail.html 模板中),但通知电子邮件永远不会发送给观察者用户。
我认为我在下面的 new_post 函数中做错了,我改编自这段代码 ,用于在用户评论帖子时发送电子邮件,这确实有效。非常感谢任何帮助。
models.py(相关部分):
from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.db.models import signals
from notification import models as notification
class Post(models.Model):
author = models.ForeignKey(User, related_name="added_posts")
observers = models.ManyToManyField(User, verbose_name=_("Observers"), related_name='observers+', blank=True, null=True)
# send notification to Post observers
def create_notice_types(app, created_models, verbosity, **kwargs):
notification.create_notice_type("new_post", "New post created", "A new post has been created")
signals.post_syncdb.connect(create_notice_types, sender=notification)
def new_post(sender, instance, created, **kwargs):
context = {
'observer': instance,
'site': Site.objects.get_current(),
}
recipients = []
pk=instance._get_pk_val()
for observer in instance.observers.all().distinct():
if observer.user not in recipients:
recipients.append(observer.user)
notification.send(recipients, 'new_post', context)
signals.post_save.connect(
new_post, sender=models.get_model(
'blog', 'Post'), dispatch_uid="pkobservers")
views.py(相关部分):
@login_required
def add(request, form_class=PostForm, template_name="blog/post_add.html"):
post_form = form_class(request)
if request.method == "POST" and post_form.is_valid():
post = post_form.save(commit=False)
post.author = request.user
post_form.save()
post_form.save_m2m()
return redirect("blog_user_post_detail",
username=request.user.username, slug=post.slug)
return render_to_response(template_name,
{"post_form": post_form}, context_instance=RequestContext(request))
编辑:
也试过这个(在删除 blog_post 和 blog_post_observers 表后,manage.py syncdb
再次运行,但仍然不起作用):
模型.py
class Post(models.Model):
# ....
observers = models.ManyToManyField(User, verbose_name=_("Observers"), related_name='observers+')
def new_post(sender, instance, created, **kwargs):
context = {
'observer': instance,
'site': Site.objects.get_current(),
}
recipients = instance.observers.all()
pk=instance._get_pk_val()
notification.send(recipients, 'new_post', context)
signals.post_save.connect(new_post, sender=models.get_model('blog', 'Post'), dispatch_uid="pkobservers")
编辑 2 2013 年 6 月 27 日星期四:11:48:49 意大利时间:
当我使用以下视图编辑/更新帖子时,通知电子邮件确实有效:
视图.py
@login_required
def edit(request, id, form_class=PostForm, template_name="blog/post_edit.html"):
post = get_object_or_404(Post, id=id)
if post.author != request.user:
request.user.message_set.create(message="You can't edit items that aren't yours")
return redirect("desk")
post_form = form_class(request, instance=post)
if request.method == "POST" and post_form.is_valid():
post = post_form.save(commit=False)
post.updated_at = datetime.now()
post_form.save()
post_form.save_m2m()
messages.add_message(request, messages.SUCCESS, message=_("Successfully updated post '%s'") % post.title)
return redirect("blog_user_post_detail", username=request.user.username, slug=post.slug)
return render_to_response(template_name, {"post_form": post_form, "post": post}, context_instance=RequestContext(request))