已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。
20天前关门了。
Improve this question的
我正在为我的课程做一个项目,用户可以向上或向下投票。我正在使用Django,最近才开始学习它。
我有一个问题,以获得过去的点击和激活按钮分开,因为我的downvote按钮只是激活upvote。
我的模板:
<div class="row">
<div class="col-1">
<strong>
{% if user.is_authenticated %}
<form action="{% url 'post_vote' post.slug %}" class="d-inline" method="POST">
{% csrf_token %}
{% if liked %}
<button type="submit" name="blogpost_id" value="{{post.slug}}" id="btn-like" class="btn-like"><i
class="fa-solid fa-circle-up" style="color: #1ea427;"></i></button>
{% else %}
<button type="submit" name="blogpost_id" value="{{post.slug}}" id="btn-like" class="btn-like"><i
class="fa-regular fa-circle-up"></i></button>
{% endif %}
</form>
{% else%}
<span class="text-scondary"><i class="fa-regular fa-circle-up"></i></span>
{% endif %}
<span class="text-secondary">{{ post.number_of_up_votes}}</span>
</strong>
</div>
<div class="col-1">
<strong>
{% if user.is_authenticated %}
<form action="{% url 'post_vote' post.slug %}" class="d-inline" method="POST">
{% csrf_token %}
{% if disliked %}
<button type="submit" name="blogdownpost_id" value="{{post.slug}}" id="btn-like" class="btn-like"><i
class="fa-solid fa-circle-down" style="color: #ff0000;"></i></button>
{% else %}
<button type="submit" name="blogdownpost_id" value="{{post.slug}}" id="btn-like" class="btn-like"><i
class="fa-regular fa-circle-down"></i></button>
{% endif %}
</form>
{% else%}
<span class="text-scondary"><i class="fa-regular fa-circle-down"></i></span>
{% endif %}
<span class="text-secondary">{{ post.number_of_down_votes}}</span>
</strong>
</div>
<div class="col-1">
{% with comments.count as total_comments %}
<strong class="text-secondary"><i class="far fa-comments"></i>
<!-- Our total_comments variable goes before the closing strong tag -->
{{ total_comments }}</strong>
{% endwith %}
</div>
</div>
字符串
我的观点:
from django.shortcuts import render, get_object_or_404, reverse
from django.views import generic, View
from django.http import HttpResponseRedirect
from .models import Post
from .forms import CommentForm
class PostList(generic.ListView):
model = Post
queryset = Post.objects.filter(status=1).order_by('-created_on')
template_name = 'index.html'
paginate_by = 6
class PostDetail(View):
def get(self, request, slug, *args, **kwargs):
queryset = Post.objects.filter(status=1)
post = get_object_or_404(queryset, slug=slug)
comments = post.comments.filter(approved=True).order_by('created_on')
liked = False
disliked = False
if post.up_vote.filter(id=self.request.user.id).exists():
liked = True
elif post.down_vote.filter(id=self.request.user.id).exists():
disliked = True
return render(
request,
"post_detail.html",
{
"post": post,
"comments": comments,
"commented": False,
"liked": liked,
"disliked": disliked,
"comment_form": CommentForm(),
},
)
def post(self, request, slug, *args, **kwargs):
queryset = Post.objects.filter(status=1)
post = get_object_or_404(queryset, slug=slug)
comments = post.comments.filter(approved=True).order_by('-created_on')
liked = False
disliked = False
if post.up_vote.filter(id=self.request.user.id).exists():
liked = True
elif post.down_vote.filter(id=self.request.user.id).exists():
disliked = True
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
comment_form.instance.email = request.user.email
comment_form.instance.name = request.user.username
comment = comment_form.save(commit=False)
comment.post = post
comment.save()
else:
comment_form = CommentForm()
return render(
request,
"post_detail.html",
{
"post": post,
"comments": comments,
"commented": True,
"liked": liked,
"disliked": disliked,
"comment_form": CommentForm(),
},
)
class PostVote(View):
def post(self, request, slug):
post = get_object_or_404(Post, slug=slug)
if post.up_vote.filter(id=request.user.id).exists():
post.up_vote.remove(request.user)
else:
post.up_vote.add(request.user)
if post.down_vote.filter(id=request.user.id).exists():
post.down_vote.remove(request.user)
else:
post.down_vote.add(request.user)
return HttpResponseRedirect(reverse('post_detail', args=[slug]))
型
我的模型:
from django.db import models
from django.contrib.auth.models import User
from cloudinary.models import CloudinaryField
STATUS = ((0, "Draft"), (1, "Published"))
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="news_posts")
updated_on = models.DateTimeField(auto_now=True)
content = models.TextField()
featured_image = CloudinaryField('image', default='placeholder')
excerpt = models.TextField(blank=True)
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
up_vote = models.ManyToManyField(User, related_name='news_up_vote', blank=True)
down_vote = models.ManyToManyField(User, related_name='news_down_vote', blank=True)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
def number_of_up_votes(self):
return self.up_vote.count()
def number_of_down_votes(self):
return self.down_vote.count()
型
我试图改变多个东西,如在定位和措辞,并观看了许多视频。
1条答案
按热度按时间5ssjco0h1#
我已经更新了你的模板中的upvote形式,包括一个隐藏字段来表示投票类型
字符串
我还更新了你的否决表,包括一个隐藏字段,以表示投票的类型
型
我已经更新了您的PostView类以使用该隐藏文件的值
型