我正在创建一个简单的应用程序,您可以在其中投票和支持不同的帖子。我在Python / Django中构建它...我通过将用户ID和注释ID存储到Upvote模型中获得upvote / downvote计数,并且通过关联计数为1.它打印出正确的数字。查看除了打印计数的次数被投票的次数。例如,如果它的1将打印:1,但是如果它的3将打印:3 3 3.如何更改我的逻辑以便它只打印出一次数字?
查看页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Topic</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<a href="/dashboard">Dashboard</a>
<a href="/logout">Logout</a>
<hr>
<a href="/users/{{topic.user.id}}/">{{topic.user.first_name}} </a>posted a topic:
<hr>
Topic: {{topic.topic}}
<br>
Description: {{topic.description}}
<hr>
<h3>Post your answer</h3>
<form action="/topic/{{topic.id}}/post" method="post">
{%csrf_token%}
<textarea name="comment" id="" cols="20" rows="2" required style="resize:none"></textarea>
<br>
<input type="submit" value="Post">
</form>
<hr>
{% for comment in comments %}
<a href="/users/{{comment.user_id}}/">{{comment.user.first_name}}</a>: {{comment.comment}}
<br>
<a href="/delete/{{comment.id}}/">Delete Post</a>
<h5>Number of upvotes:</h5>
{% for upvote in upvotes %}
{% if upvote.comment_id == comment.id %}
{{upvotes.count}}
{%endif%}
{%endfor%}
<h5>Number of downvotes: </h5>
{% for downvote in downvotes %}
{% if downvote.comment_id == comment.id %}
{{downvotes.count}}
{%endif%}
{%endfor%}
<hr>
<a href="/upvote/{{comment.id}}/"><button>Upvote</button></a>
<a href="/downvote/{{comment.id}}/"><button>Downvote</button></a>
<br>
<form action="/idea/{{comment.id}}/" method="post">
{%csrf_token%}
<textarea name="idea" id="" cols="20" rows="2" required style="resize:none"></textarea>
<br>
<input type="submit" value="Comment">
</form>
{% for idea in ideas %}
{% if idea.comment_id == comment.id %}
<a href="/users/{{idea.user_id}}/">{{idea.user}}</a>
says: {{idea.idea}}
<a href="/delete/{{idea.id}}/comment">Delete Comment</a>
{%endif%}
{%endfor%}
{%endfor%}
</div>
</body>
</html>
答案 0 :(得分:1)
鉴于你的代码
{% for upvote in upvotes %}
{% if upvote.comment_id == comment.id %}
{{upvotes.count}}
{%endif%}
{%endfor%}
您最终打印upvotes.count
upvotes
次。你可以完全摆脱整个for循环,只需使用{{upvotes.count}}
。
答案 1 :(得分:0)
现在这是我的函数...试图在函数中递增一个名为counter ...的函数。在模型中将它作为一个整数字段,默认值为0
def upvote(request, comment_id):
print "upvoting comment"
user = User.objects.get(id=request.session['user_id'])
comment = Comment.objects.get(id=comment_id)
upvote = Upvote()
counter = 1
upvote.comment = comment
upvote.user = user
upvote.created_at = timezone.now()
upvote.counter = upvote.counter + 1
upvote.save()
print upvote.counter
return redirect('/dashboard')
和模型......
class Upvote(models.Model):
user = models.ForeignKey(User, related_name="upvote_user", null=True)
comment = models.ForeignKey(Comment, related_name="comment_upvote", null=True)
counter = models.IntegerField(null=True, blank=False, default=0)
created_at = models.DateField(null=True)
class Meta:
db_table = 'upvote'
views.py为模板渲染函数..
def show_topic(request, topic_id):
print "Showing a topic"
topic = Topic.objects.get(id=topic_id)
comments = Comment.objects.all().filter(topic=topic)
upvotes = Upvote.objects.all()
ideas = Idea.objects.all()
downvotes = Downvote.objects.all()
context = {'upvotes': upvotes, 'ideas': ideas, 'comments': comments, 'topic': topic,'downvotes': downvotes}
return render(request, 'discussionboard/show.html', context)