我有一個問題,像按鈕它根本不工作。在模板我用 {{ 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
請顯示您的'Post'模型。 – Alasdair
我編輯了這個問題,謝謝你的幫助:) – zbakh
你想讓'post_like'和'dislike_post'視圖重定向到哪個網址? – Alasdair