Reputation: 85
I am making a simple app where you downvote and upvote different posts. I am buildig it in Python/Django...i get the upvote/downvote count by storing a user id and a comment id into the Upvote model and through the association there is a count of 1. It's printing out the right number in the view except for its printing out the count the number of times that is is upvoted. For example, if its 1 it will print : 1, but if its 3 it will print : 3 3 3. How do i change my logic so that it only prints out the number once?
View page:
<!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>
Upvotes: 1
Views: 217
Reputation: 85
This is my function now...trying to increment a variable in the function called counter..i have it in the model as an integer field with a default=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')
and the model...
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 render function for template..
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)
Upvotes: 0
Reputation: 7716
Given your code
{% for upvote in upvotes %}
{% if upvote.comment_id == comment.id %}
{{upvotes.count}}
{%endif%}
{%endfor%}
you end up printing upvotes.count
for upvotes
times. You can completely get rid of the whole for loop and just use {{upvotes.count}}
.
Upvotes: 1