添加“if”语句,当点赞数达到一定值时发送电子邮件

我正在尝试添加 IF 语句,以便当点赞数达到 2 时发送电子邮件。我已经编写了代码,但它不起作用,我正在尝试以下代码,但它不起作用。


我的问题是如何添加 if 语句,以便当任何具有 like more the 2 的项目时,都会向电子邮件发送特定的电子邮件。


这是 models.py


class Post(models.Model):

    title = models.CharField(max_length=100, unique=True)

    likes = models.ManyToManyField(

        User, related_name='liked', blank=True)


    def __str__(self):

        return self.title


    def total_likes(self):

        return self.likes.count()

这是views.py


class PostDetailView(DetailView):

    model = Post

    template_name = "post_detail.html"


    def get_context_data(self, *args, **kwargs):

        context = super(PostDetailView, self).get_context_data()

        post = get_object_or_404(Post, slug=self.kwargs['slug'])

        comments = Comment.objects.filter(

            post=post, reply=None).order_by('-id')

        total_likes = post.total_likes()

        liked = False

        if post.likes.filter(id=self.request.user.id).exists():

            liked = True


        if self.request.method == 'POST':

            comment_form = CommentForm(self.request.POST or None)

            if comment_form.is_valid():

                content = self.request.POST.get('content')

                reply_id = self.request.POST.get('comment_id')

                comment_qs = None


                if reply_id:

                    comment_qs = Comment.objects.get(id=reply_id)

                comment = Comment.objects.create(

                    post=post, user=self.request.user, content=content, reply=comment_qs)

                comment.save()

                return HttpResponseRedirect("post_detail.html")

        else:

            comment_form = CommentForm()


        context["total_likes"] = total_likes

        context["liked"] = liked

        context["comments"] = comments

        context["comment_form"] = comment_form

        return context


    def get(self, request, *args, **kwargs):

        res = super().get(request, *args, **kwargs)

        self.object.incrementViewCount()

        return res


倚天杖
浏览 100回答 1
1回答

慕尼黑5688855

如果您希望这种情况在任何地方发生,我建议向模型save()的方法添加一个覆盖Post,并在它增加到两个喜欢时向那里发送电子邮件。您还可以使用 Django 的send_mail函数,它是发送电子邮件的一个很好的包装器。胖模型、瘦视图、愚蠢模板是 Django 最佳实践的口头禅。也许是这样的:from django.core.mail import send_mailclass Post(models.Model):&nbsp; &nbsp; title = models.CharField(max_length=100, unique=True)&nbsp; &nbsp; likes = models.ManyToManyField(&nbsp; &nbsp; &nbsp; &nbsp; User, related_name='liked', blank=True)&nbsp; &nbsp; def __str__(self):&nbsp; &nbsp; &nbsp; &nbsp; return self.title&nbsp; &nbsp; def save(self, *args, **kwargs):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if self.total_likes() == 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; send_mail(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "my subject",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "my text message",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "myemail@myhost.com",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["myuser@theirhost.com"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html_message="<h1>My HTML Message</h1>",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; super().save(*args, **kwargs)&nbsp;&nbsp; &nbsp; def total_likes(self):&nbsp; &nbsp; &nbsp; &nbsp; return self.likes.count()祝你好运!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python