Like button not recording likes in the back end

huangapple go评论60阅读模式
英文:

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, &#39;viewblog&#39;, {&#39;post&#39;: post})

urls

path(&#39;view-blog/&lt;slug:slug&gt;/&#39;, views.ViewBlog, name=&#39;viewblog&#39;),
path(&#39;like/&lt;slug:slug&gt;&#39;, views.BlogLike, name=&#39;likepost&#39;),

html

&lt;form action=&quot;{% url &#39;viewblog&#39; blog.slug %}&quot; method=&quot;POST&quot;&gt;
    {% csrf_token %}
    &lt;button class=&quot;btn btn-outline-secondary rounded-0 custom-button&quot; id=&quot;like&quot; type=&quot;sumbit&quot; name=&quot;blog_id&quot; value=&quot;{{ blog.slug }}&quot;&gt;Like&lt;/button&gt;
&lt;/form&gt;   

EDIT

class BlogPost(models.Model):
    title = models.CharField(max_length=100, null=False, blank=False, default=&quot;&quot;,)
    text = RichTextUploadingField(null=True, blank=True, default=&quot;text&quot;)
    featured_text = models.TextField(max_length=550, null=True, blank=True, default=&quot;text&quot;)
    image = models.ImageField(null=True, blank=True, upload_to=&quot;images&quot;, default=&quot;default.png&quot;)
    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=&#39;likes&#39;)
    
    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(&#39;blog-id&#39;)
        post = BlogPost.objects.get(slug=post_id)
        post.likes.add(request.user)
    
        return HttpResponseRedirect(reverse(&#39;viewblog&#39;, args=[post_id]))

html

&lt;form action=&quot;{% url &#39;likepost&#39; blog.slug %}&quot; method=&quot;POST&quot;&gt;
    {% csrf_token %}
    &lt;button class=&quot;btn btn-outline-secondary rounded-0 custom-button&quot; id=&quot;like&quot; type=&quot;sumbit&quot; name=&quot;blog-id&quot; value=&quot;{{ blog.slug }}&quot;&gt;Like&lt;i class=&quot;fa-solid fa-heart&quot;&gt;&lt;/i&gt;&lt;/button&gt; - {{ blog.total_likes }} Likes
&lt;/form&gt;  

urls

 path(&#39;like/&lt;slug:slug&gt;&#39;, views.BlogLike, name=&#39;likepost&#39;),

models

likes = models.ManyToManyField(User, related_name=&#39;likes&#39;)

  def total_likes(self):
        return self.likes.count()   #this is to count amount of likes a post has.

huangapple
  • 本文由 发表于 2023年2月24日 04:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定