模板中的点赞数没有增加,但在管理中有效
Number of Likes is not increasing in the template but it's works in admin
我按照 youtube 中的教程只是为了向我的博客应用程序添加一个赞按钮,但模板中的赞数并没有增加。但是当我突出显示用户并在管理区域中点击保存时,它会增加。我的意思是它在管理员中工作正常但在模板中工作不正常。
我该如何设置?
模特:
class Photo(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.CharField(max_length=30,null=True, blank=False)
image = models.ImageField(null=False, blank=False)
description = models.TextField(null=True)
date_added = models.DateTimeField(auto_now_add=True)
likes = models.ManyToManyField(User, related_name='blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return str(self.category)
观点:
def like(request, pk):
post = get_object_or_404(Photo, id=request.GET.get('post_id'))
post.Likes.add(request.user)
return HttpResponseRedirect(reverse('view', args=[str(pk)]))
def viewPhoto(request, pk):
post = get_object_or_404(Photo, id=pk)
photo = Photo.objects.get(id=pk)
stuff = get_object_or_404(Photo, id=pk)
total_likes = stuff.total_likes()
return render(request, 'photo.html', {'photo': photo, 'post': post, 'total_likes':
total_likes})
模板:
<form action="{% url 'Photo' photo.id %}" method="POST">
{% csrf_token %}
{{ total_likes }}
<button type="submit", name="post_id" value="{{ post.id }}">Touch</button>
</form>
网址:
path('', views.login, name='login'),
path('home', views.home, name='home'),
path('view/<str:pk>/', views.viewPhoto, name='Photo'),
path('post/create', views.PostCreativeView.as_view(), name='post_create'),
path('register', views.register, name='register'),
path('comment/<str:pk>/', views.comment, name='comment'),
path('like/<str:pk>/', views.like, name='like_post'),
好吧,通过简单的像这样的简单操作来获取表单中喜欢的对象的数量非常简单:
# In your view add s to the post variable
def viewPhoto(request, pk):
posts = get_object_or_404(Photo, id=pk)
photo = Photo.objects.get(id=pk)
stuff = get_object_or_404(Photo, id=pk)
total_likes = stuff.total_likes()
return render(request, 'photo.html', {'photo': photo, 'posts': posts, 'total_likes':
total_likes})
{% for post in posts %}
<form action="{% url 'like_post' photo.id %}" method="POST">
{% csrf_token %}
{{ post.likes.count }} # this would count and give you the total number of likes
<button type="submit", name="post_id" value="{{ post.id }}">Touch</button>
</form>
{% endfor %}
# OR
{% for post in posts %}
<form action="{% url 'like_post' photo.id %}" method="POST">
{% csrf_token %}
{{ total_likes }} # this would count and give you the total number of likes
<button type="submit", name="post_id" value="{{ post.id }}">Touch</button>
</form>
{% endfor %}
我按照 youtube 中的教程只是为了向我的博客应用程序添加一个赞按钮,但模板中的赞数并没有增加。但是当我突出显示用户并在管理区域中点击保存时,它会增加。我的意思是它在管理员中工作正常但在模板中工作不正常。
我该如何设置?
模特:
class Photo(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
category = models.CharField(max_length=30,null=True, blank=False)
image = models.ImageField(null=False, blank=False)
description = models.TextField(null=True)
date_added = models.DateTimeField(auto_now_add=True)
likes = models.ManyToManyField(User, related_name='blog_posts')
def total_likes(self):
return self.likes.count()
def __str__(self):
return str(self.category)
观点:
def like(request, pk):
post = get_object_or_404(Photo, id=request.GET.get('post_id'))
post.Likes.add(request.user)
return HttpResponseRedirect(reverse('view', args=[str(pk)]))
def viewPhoto(request, pk):
post = get_object_or_404(Photo, id=pk)
photo = Photo.objects.get(id=pk)
stuff = get_object_or_404(Photo, id=pk)
total_likes = stuff.total_likes()
return render(request, 'photo.html', {'photo': photo, 'post': post, 'total_likes':
total_likes})
模板:
<form action="{% url 'Photo' photo.id %}" method="POST">
{% csrf_token %}
{{ total_likes }}
<button type="submit", name="post_id" value="{{ post.id }}">Touch</button>
</form>
网址:
path('', views.login, name='login'),
path('home', views.home, name='home'),
path('view/<str:pk>/', views.viewPhoto, name='Photo'),
path('post/create', views.PostCreativeView.as_view(), name='post_create'),
path('register', views.register, name='register'),
path('comment/<str:pk>/', views.comment, name='comment'),
path('like/<str:pk>/', views.like, name='like_post'),
好吧,通过简单的像这样的简单操作来获取表单中喜欢的对象的数量非常简单:
# In your view add s to the post variable
def viewPhoto(request, pk):
posts = get_object_or_404(Photo, id=pk)
photo = Photo.objects.get(id=pk)
stuff = get_object_or_404(Photo, id=pk)
total_likes = stuff.total_likes()
return render(request, 'photo.html', {'photo': photo, 'posts': posts, 'total_likes':
total_likes})
{% for post in posts %}
<form action="{% url 'like_post' photo.id %}" method="POST">
{% csrf_token %}
{{ post.likes.count }} # this would count and give you the total number of likes
<button type="submit", name="post_id" value="{{ post.id }}">Touch</button>
</form>
{% endfor %}
# OR
{% for post in posts %}
<form action="{% url 'like_post' photo.id %}" method="POST">
{% csrf_token %}
{{ total_likes }} # this would count and give you the total number of likes
<button type="submit", name="post_id" value="{{ post.id }}">Touch</button>
</form>
{% endfor %}