英文:
Like button not recording likes in the back end
问题
以下是您要翻译的内容:
views
def BlogLike(request, slug):
post = get_object_or_404(BlogPost, id=request.POST.get(slug))
post.likes.add(request.user)
return render(request, 'viewblog', {'post': post})
urls
path('view-blog/<slug:slug>/', views.ViewBlog, name='viewblog'),
path('like/<slug:slug>', views.BlogLike, name='likepost'),
html
<form action="{% url 'viewblog' blog.slug %}" method="POST">
{% csrf_token %}
<button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="submit" name="blog_id" value="{{ blog.slug }}">Like</button>
</form>
EDIT
class BlogPost(models.Model):
title = models.CharField(max_length=100, null=False, blank=False, default="")
text = RichTextUploadingField(null=True, blank=True, default="text")
featured_text = models.TextField(max_length=550, null=True, blank=True, default="text")
image = models.ImageField(null=True, blank=True, upload_to="images", default="default.png")
date = models.DateField(auto_now_add=True)
published = models.BooleanField(default=False)
featured = models.BooleanField(default=False)
slug = models.SlugField()
likes = models.ManyToManyField(User, related_name='likes')
def save(self, *args, **kwargs):
self.slug = self.slug or slugify(self.title)
super().save(*args, **kwargs)
def __str__(self):
return self.title
英文:
Button successfully links back to current blog page but the likes are recording in the back end, what am I doing wrong here?
views
def BlogLike (request, slug):
post = get_object_or_404(BlogPost, id=request.POST.get(slug))
post.likes.add(request.user)
return render(request, 'viewblog', {'post': post})
urls
path('view-blog/<slug:slug>/', views.ViewBlog, name='viewblog'),
path('like/<slug:slug>', views.BlogLike, name='likepost'),
html
<form action="{% url 'viewblog' blog.slug %}" method="POST">
{% csrf_token %}
<button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="sumbit" name="blog_id" value="{{ blog.slug }}">Like</button>
</form>
EDIT
class BlogPost(models.Model):
title = models.CharField(max_length=100, null=False, blank=False, default="",)
text = RichTextUploadingField(null=True, blank=True, default="text")
featured_text = models.TextField(max_length=550, null=True, blank=True, default="text")
image = models.ImageField(null=True, blank=True, upload_to="images", default="default.png")
date = models.DateField(auto_now_add=True)
published = models.BooleanField(default=False)
featured = models.BooleanField(default=False)
slug = models.SlugField()
likes = models.ManyToManyField(User, related_name='likes')
def save(self, *args, **kwargs):
self.slug = self.slug or slugify(self.title)
super().save(*args, **kwargs)
def __str__(self):
return self.title
答案1
得分: 0
这是我的功能性代码。
views
def BlogLike(request, slug):
post_id = request.POST.get('blog-id')
post = BlogPost.objects.get(slug=post_id)
post.likes.add(request.user)
return HttpResponseRedirect(reverse('viewblog', args=[post_id]))
html
<form action="{% url 'likepost' blog.slug %}" method="POST">
{% csrf_token %}
<button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="submit" name="blog-id" value="{{ blog.slug }}">喜欢<i class="fa-solid fa-heart"></i></button> - {{ blog.total_likes }} 喜欢
</form>
urls
path('like/<slug:slug>', views.BlogLike, name='likepost'),
models
likes = models.ManyToManyField(User, related_name='likes')
def total_likes(self):
return self.likes.count() #这是用来统计帖子有多少喜欢的数量。
英文:
This is my functional code.
views
def BlogLike (request, slug):
post_id = request.POST.get('blog-id')
post = BlogPost.objects.get(slug=post_id)
post.likes.add(request.user)
return HttpResponseRedirect(reverse('viewblog', args=[post_id]))
html
<form action="{% url 'likepost' blog.slug %}" method="POST">
{% csrf_token %}
<button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="sumbit" name="blog-id" value="{{ blog.slug }}">Like<i class="fa-solid fa-heart"></i></button> - {{ blog.total_likes }} Likes
</form>
urls
path('like/<slug:slug>', views.BlogLike, name='likepost'),
models
likes = models.ManyToManyField(User, related_name='likes')
def total_likes(self):
return self.likes.count() #this is to count amount of likes a post has.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论