我正在尝试添加 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
慕尼黑5688855
相关分类