登录时的初始页面是ReviewList。它显示每个Review项目,每个评论都有一个Like按钮。目标是,当您喜欢评论时,它会将Like计数增加1并重定向到同一模板。执行此操作后,Like按钮将变为不同的Button,然后在单击时将Like计数减1。实际的Like / Different逻辑工作正常但我在将重定向的GET请求传递回ReviewList时遇到了麻烦。最后,我想将上下文['likes']传递给模板,这样我就可以根据它是显示Like还是Different来做条件。这就是我所拥有的:
views.py
class ReviewList(ListView):
model = Review
def get_context_data(self, **kwargs):
context = super(ReviewList, self).get_context_data(**kwargs)
context['now'] = timezone.now()
""" Checks if user has liked any reviews. If the user has, then context['liked'] is true, else false. This
will get passed in to template so that the like button will turn into a unlike button and vice versa """
try:
user_liked = Like.objects.get(user=user, review_id=self.request.GET['review_id']))
except:
user_liked = None
if user_liked:
liked = True
else:
liked = False
context['liked'] = liked
return context
...
...
def like(request, id):
if request.method == 'POST':
user = request.user
review = get_object_or_404(Review, id=id)
like = Like.objects.get_or_create(review_id=review.id)
try:
user_liked = Like.objects.get(user=user, review_id=review.id)
except:
user_liked = None
if user_liked:
user_liked.total_likes -= 1
user_liked.user.remove(request.user)
user_liked.save()
else:
list(like)
like[0].user.add(request.user)
like[0].total_likes += 1
like[0].save()
return redirect(reverse_lazy('reviews:review_list'), review.id)
urls.py
from django.conf.urls import patterns, url
from apps.reviews import views
urlpatterns = patterns('',
url(r'^(?P<review_id>\d+)|$', views.ReviewList.as_view(), name='review_list'),
url(r'^new/$', views.ReviewCreate.as_view(), name='new'),
url(r'^(?P<pk>\d+)/$', views.ReviewDetail.as_view(), name='show'),
url(r'^edit/(?P<pk>\d+)/$', views.ReviewUpdate.as_view(), name='edit'),
url(r'^delete/(?P<pk>\d+)/$', views.ReviewDelete.as_view(), name='delete'),
url(r'^like/(?P<id>\d+)/$', views.like, name='like'),
)
已更新
我没有让我的喜欢按钮重定向到它的当前页面,而是决定调整类似按钮。挽救很多头痛。
views.py
def like(request):
if request.method == 'GET':
user = request.user
review = get_object_or_404(Review, id=request.GET['review_id'])
like = Like.objects.get_or_create(review_id=review.id)
try:
user_liked = Like.objects.get(user=user, review_id=review.id)
except:
user_liked = None
if user_liked:
user_liked.total_likes -= 1
user_liked.user.remove(request.user)
user_liked.save()
likes = user_liked.total_likes
else:
list(like)
like[0].user.add(request.user)
like[0].total_likes += 1
like[0].save()
likes = like[0].total_likes
return HttpResponse(likes)
urls.py
from django.conf.urls import patterns, url
from apps.reviews import views
urlpatterns = patterns('',
url(r'^$', views.ReviewList.as_view(), name='review_list'),
url(r'^new/$', views.ReviewCreate.as_view(), name='new'),
url(r'^(?P<pk>\d+)/$', views.ReviewDetail.as_view(), name='show'),
url(r'^edit/(?P<pk>\d+)/$', views.ReviewUpdate.as_view(), name='edit'),
url(r'^delete/(?P<pk>\d+)/$', views.ReviewDelete.as_view(), name='delete'),
url(r'^like$', views.like, name='like'),
)
添加了review.js
$(function (){
$('.btn.active').click(function(e){
e.preventDefault();
var rev_id = $(this).attr('data-attr');
$.get('/reviews/like', { review_id: rev_id}, function(data){
$('td#review_' + rev_id).html(data);
});
});
});
来自review_list.html的摘录
<td id='review_{{review.id}}' class="likes">
{% if review.like_set.all|length != 0 %}
{% for like in review.like_set.all %}
{{ like.total_likes }}
{% endfor %}
{% else %}
0
{% endif %}
</td>