将按钮已被用户点赞时,将“Like”更改为“Unlike”。

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

Turn 'Like' to 'Unlike' if button is already liked by user

问题

我已经将代码部分翻译好了,以下是翻译后的代码:

def BlogLike(request, slug):
    post_id = request.POST.get('blog-id')
    post = BlogPost.objects.get(slug=post_id)
    
    liked = False

    if post.likes.filter(id=request.user.id).exists():
        post.likes.remove(request.user)
        liked = False
    else:
        post.likes.add(request.user)
        liked = True
    return HttpResponseRedirect(reverse('viewblog', args=[post_id]))
<form action="{% url 'likepost' blog.slug %}" method="POST">
    {% csrf_token %}
    {% if blog.liked is True %}
        <button class="btn btn-outline-secondary rounded-0 custom-button" id="like" type="sumbit" name="blog-id" value="{{ blog.slug }}">Unlike<i class="fa-solid fa-heart-crack"></i></button> - {{ blog.total_likes }} Likes
    {% else %}
        <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
    {% endif %}
</form>

希望这有所帮助。如果您需要进一步的信息或帮助,请随时告诉我。

英文:

I have the 'Like' button working which adds the currently logged in user to database once button is pressed, but the button doesn't turn to 'Unlike'.

If the button is pressed a second time the 'Like' is removed from the database.

So the key issue is getting the button to change.

I'm thinking I would need a variable for liked_status some where possibly? But wouldn't know how to implement this.

Views

def BlogLike (request, slug):
    post_id = request.POST.get(&#39;blog-id&#39;)
    post = BlogPost.objects.get(slug=post_id)
    
    liked = False

    if post.likes.filter(id=request.user.id).exists():
        post.likes.remove(request.user)
        liked = False
    else:
        post.likes.add(request.user)
        liked = True
    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 %}
    {% if blog.liked is True %}
        &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;Unlike&lt;i class=&quot;fa-solid fa-heart-crack&quot;&gt;&lt;/i&gt;&lt;/button&gt; - {{ blog.total_likes }} Likes
    {% else %}
        &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
    {% endif %}
&lt;/form&gt;    

答案1

得分: 1

以下是您要翻译的内容:

view.py

@login_required  
def BlogLike(request, slug):
    post = get_object_or_404(BlogPost, slug=slug)
    liked = False

    if request.method == &#39;POST&#39;:
        if post.likes.filter(id=request.user.id).exists():
            post.likes.remove(request.user)
            liked = False
        else:
            post.likes.add(request.user)
            liked = True

    return HttpResponseRedirect(reverse(&#39;viewblog&#39;, args=[post.slug]))

and in html

{% csrf_token %}
&lt;form action=&quot;{% url &#39;likepost&#39; post.slug %}&quot; method=&quot;POST&quot;&gt;
  &lt;button type=&quot;submit&quot; id=&quot;like&quot; value=&quot;{{ post.slug }}&quot; class=&quot;btn btn-outline-secondary rounded-0 custom-button&quot;&gt;
    {% if post.likes.filter(id=request.user.id).exists %} 
      Unlike &lt;i class=&quot;fa-solid fa-heart-crack&quot;&gt;&lt;/i&gt; 
    {% else %} 
      Like &lt;i class=&quot;fa-solid fa-heart&quot;&gt;&lt;/i&gt; 
    {% endif %} 
  &lt;/button&gt;
&lt;/form&gt;
英文:

You can try the following way:

view.py

@login_required  
def BlogLike(request, slug):
    post = get_object_or_404(BlogPost, slug=slug)
    liked = False

    if request.method == &#39;POST&#39;:
        if post.likes.filter(id=request.user.id).exists():
            post.likes.remove(request.user)
            liked = False
        else:
            post.likes.add(request.user)
            liked = True

    return HttpResponseRedirect(reverse(&#39;viewblog&#39;, args=[post.slug]))

and in html

{% csrf_token %}
&lt;form action=&quot;{% url &#39;likepost&#39; post.slug %}&quot; method=&quot;POST&quot;&gt;
  &lt;button type=&quot;submit&quot; id=&quot;like&quot; value=&quot;{{ post.slug }}&quot; class=&quot;btn btn-outline-secondary rounded-0 custom-button&quot;&gt;
    {% if post.likes.filter(id=request.user.id).exists %}
      Unlike &lt;i class=&quot;fa-solid fa-heart-crack&quot;&gt;&lt;/i&gt;
    {% else %}
      Like &lt;i class=&quot;fa-solid fa-heart&quot;&gt;&lt;/i&gt;
    {% endif %}
  &lt;/button&gt;
&lt;/form&gt;

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

发表评论

匿名网友

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

确定