英文:
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('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]))
html
<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>
答案1
得分: 1
以下是您要翻译的内容:
view.py
@login_required
def BlogLike(request, slug):
post = get_object_or_404(BlogPost, slug=slug)
liked = False
if request.method == 'POST':
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.slug]))
and in html
{% csrf_token %}
<form action="{% url 'likepost' post.slug %}" method="POST">
<button type="submit" id="like" value="{{ post.slug }}" class="btn btn-outline-secondary rounded-0 custom-button">
{% if post.likes.filter(id=request.user.id).exists %}
Unlike <i class="fa-solid fa-heart-crack"></i>
{% else %}
Like <i class="fa-solid fa-heart"></i>
{% endif %}
</button>
</form>
英文:
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 == 'POST':
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.slug]))
and in html
{% csrf_token %}
<form action="{% url 'likepost' post.slug %}" method="POST">
<button type="submit" id="like" value="{{ post.slug }}" class="btn btn-outline-secondary rounded-0 custom-button">
{% if post.likes.filter(id=request.user.id).exists %}
Unlike <i class="fa-solid fa-heart-crack"></i>
{% else %}
Like <i class="fa-solid fa-heart"></i>
{% endif %}
</button>
</form>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论