如何使用 Django 中的通用关系将喜欢添加到我的博客文章中
How to add likes to my blog posts using generic relationships in Django
我正在尝试使用通用关系在 Django 中添加 like/dislike 功能。有人可以帮助我吗?
我的post模特
class Post(models.Model):
title = models.CharField(max_length=225)
post_image = models.ImageField(null=True, blank=True, upload_to="images/")
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
post_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title + ' | ' + str(self.author)
class Meta:
ordering = ['-post_date',]
def get_absolute_url(self):
return reverse('post-detail', args=(str(self.id)),)
提前致谢!
创建一个新的 class,如下所示:
class LikedPost(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
liked_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.post.title + " liked by " + self.user.username
然后,每当用户点击 like
按钮时,创建一个新的 LikedPost
如果不存在 user
喜欢 post
用于 like
功能, 否则删除 LikedPost
用于 dislike
函数。
@login_required
def hit_like_button(request, pk):
# Get the id of post
post = get_object_or_404(Post, pk=pk)
# Create a new LikedPost if this post is not liked by this user.
liked_post, created = LikedPost.objects.get_or_create(
post=post,
user=request.user
)
# If not created new LikedPost <=> exist LikedPost = dislike.
if not created: #If get_or_create() function not created a new LikedPost
liked_post.delete()
messages.info(request, "This post was disliked.")
else: # If created a new LikedPost => this user did not like this post
liked_post.save()
messages.info(request, "This post was liked.")
return redirect('home')
我正在尝试使用通用关系在 Django 中添加 like/dislike 功能。有人可以帮助我吗?
我的post模特
class Post(models.Model):
title = models.CharField(max_length=225)
post_image = models.ImageField(null=True, blank=True, upload_to="images/")
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
post_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title + ' | ' + str(self.author)
class Meta:
ordering = ['-post_date',]
def get_absolute_url(self):
return reverse('post-detail', args=(str(self.id)),)
提前致谢!
创建一个新的 class,如下所示:
class LikedPost(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
liked_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.post.title + " liked by " + self.user.username
然后,每当用户点击 like
按钮时,创建一个新的 LikedPost
如果不存在 user
喜欢 post
用于 like
功能, 否则删除 LikedPost
用于 dislike
函数。
@login_required
def hit_like_button(request, pk):
# Get the id of post
post = get_object_or_404(Post, pk=pk)
# Create a new LikedPost if this post is not liked by this user.
liked_post, created = LikedPost.objects.get_or_create(
post=post,
user=request.user
)
# If not created new LikedPost <=> exist LikedPost = dislike.
if not created: #If get_or_create() function not created a new LikedPost
liked_post.delete()
messages.info(request, "This post was disliked.")
else: # If created a new LikedPost => this user did not like this post
liked_post.save()
messages.info(request, "This post was liked.")
return redirect('home')