2016-03-15 34 views
0

我有一個問題,像按鈕它根本不工作。在模板我用 {{ post.likes }}顯示計數喜歡和按鈕我用<a href="{% url 'post_like' pk=post.pk %}" class="btn btn-block hvr-bounce-in">Django喜歡按鈕dosen't增加和重定向我到同一頁

這是views.py

from django.http.response import HttpResponse, HttpResponseRedirect 
from django.shortcuts import render, get_object_or_404, redirect, render_to_response 
from django.template import Context 
from django.utils import timezone 
from django.views.generic import View 

from blog.forms import CommentForm, PostForm, SearchForm 
from blog.models import Post 


def post_list(request): 
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')[0:50] 
    return render(request, 'blog/index.html', {'posts': posts}) 


def post_detail(request, pk): 
    post = get_object_or_404(Post, pk=pk) 
    return render(request, 'blog/post_detail.html', {'post': post}) 


def add_comment_to_post(request, pk): 
    post = get_object_or_404(Post, pk=pk) 
    if request.method == "POST": 
     form = CommentForm(request.POST) 
     if form.is_valid(): 
      comment = form.save(commit=False) 
      comment.post = post 
      comment.save() 
      return redirect('blog.views.post_detail', pk=post.pk) 
    else: 
     form = CommentForm() 
    return render(request, 'blog/add_comment_to_post.html', {'form': form}) 


def post_new(request): 
    if request.method == "POST": 
     form = PostForm(request.POST, request.FILES) 
     if form.is_valid(): 
      post = form.save(commit=False) 
      post.author = request.user 
      post.published_date = timezone.now() 
      post.save() 
      return redirect('post_detail', pk=post.pk) 
    else: 
     form = PostForm() 
    return render(request, 'blog/add_post.html', {'form': form}) 


def search_view(request): 
    if request.method == "POST": 
     search_text = request.POST['search-form'] 
    else: 
     search_text = '' 
    posts = Post.objects.filter(title__contains=search_text) 
    return render_to_response('blog/search.html', {'post': posts}) 


def post_like(request, pk): 
    if pk: 
     post = Post.objects.get(id=pk) 
     count = post.likes 
     count += 1 
     post.likes = count 
     post.save() 
    return HttpResponseRedirect('post/%s/like/' % pk) 


def dislike_post(request, pk): 
    if pk: 
     post = Post.objects.get(id=pk) 
     post.dislikes += 1 
     post.save() 
    return HttpResponseRedirect('post/%s/like/' % pk) 

和urls.py

from django.conf.urls import url 

from blog import views 

urlpatterns = [ 
    url(r'^$', views.post_list, name='post_list'), 
    url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'), 
    url(r'^post/(?P<pk>[0-9]+)/comment/$', views.add_comment_to_post, name='add_comment_to_post'), 
    url(r'^post/new/$', views.post_new, name='post_new'), 
    url(r'^search/$', views.search_view, name='search'), 
    url(r'^post/(?P<pk>[0-9]+)/like/$', views.post_like, name='post_like'), 
    # url(r'^post/(?P<pk>[0-9]+)/dislike/$', views.dislike_post, name='post_dislike'), 
] 

編輯:對於model.py這是我使用的代碼:

class Post(models.Model): 
    author = models.ForeignKey('auth.User') 
    title = models.CharField(max_length=150) 
    text = models.TextField() 
    created_date = models.DateTimeField(
     default=timezone.now) 
    published_date = models.DateTimeField(
     blank=True, null=True) 
    image = VersatileImageField(upload_to='images') 
    tags = TaggableManager() 
    likes = models.IntegerField(default=0) 
    dislikes = models.ImageField(default=0) 
    views = models.IntegerField(default=0) 

    def publish(self): 
     self.published_date = timezone.now() 
     self.save() 

    def __str__(self): 
     return self.title 
+0

請顯示您的'Post'模型。 – Alasdair

+0

我編輯了這個問題,謝謝你的幫助:) – zbakh

+1

你想讓'post_like'和'dislike_post'視圖重定向到哪個網址? – Alasdair

回答

2

要更改視圖重定向到的url,只需將您傳遞的值更改爲HttpResponseRedirect

return HttpResponseRedirect('/post/%s/' % pk) 

請注意,網址以斜槓開頭。您可以使用reverse,防止硬編碼的URL

from django.core.urlresolvers import reverse 

def post_like(request, pk): 
    ... 
    return HttpResponseRedirect(reverse('post_detail', args=[pk]))  

我不知道爲什麼likes計數不增加。在你看來,我想補充一些打印語句,試圖找出到底是怎麼回事,例如:

from django.shortcuts import redirect 

def post_like(request, pk): 
    print("In post_like") 
    post = Post.objects.get(id=pk) 
    print("likes before: ", post.likes) 
    post.likes += 1 
    post.save() 
    print("likes after: ", post.likes) 
    return redirect('post_detail', pk=pk) 

請注意,我已經使用了redirect快捷方式以簡化代碼。您不需要if pk聲明,因爲pk是必需的參數。

+1

非常感謝,我現在用按鈕修復了問題(我忘了將{%url'post_like'pk = post.pk%}添加到類似的按鈕現在它工作) – zbakh

相關問題