Ecko
Ecko

Reputation: 191

django: Referring to a specific comment in a voting system

I'm stuck implementing a voting system for my Comments model. What I'm trying to do is similar to how the reddit commenting system works. You vote a specific comment up, and you can see a little score right next to that comment.

I feel close, I just can't figure out how to refer to the comment that was voted on.

Here's my comment model:

class Comments(models.Model):
    name = models.TextField(default='Anon')
    comment = models.TextField(max_length=2000, default='')
    vote = models.IntegerField(default=0)
    post = models.ForeignKey(Post)

    user_comments = models.ForeignKey(User, related_name='comments', null=True)

What I want to happen is when a user clicks a vote button, the vote field is altered and the score is viewed.

Here's the view logic with the comment vote buttons:

{% for c in comments %}
            <br>
            <a href="{% url 'user_profile' c.user_comments %}"> {{c.name}} </a> <br><br>
            <p> {{c.comment}} </p> </br>
            {% if user.is_authenticated %}
            <form method="POST">
                {% csrf_token %}
                    <input class='small' type='submit' value='+1' name='commentup'>
                    <input class='small' type='submit' value='-1' name='commentdown'>
            </form>
            <p> Score: {{c.vote}} </p>
            {% endif %}
        {% endfor %}

When a voteup or votedown button is pressed, a POST request is sent to my view. My confusion starts with the view logic:

def view_post(request, question_id):
    tpost = get_object_or_404(Post, pk=question_id)
    comments = tpost.comments_set.all()
    [...]
    #comment votes
        if request.POST.get('commentup',''):
            self.vote += 1
        if request.POST.get('commentdown',''):
            self.vote -=1
    [...]

Notice I use self.vote +=1, which doesn't work. What I want to do is have the vote of that particular comment go up, basically what self would do normally.

My question is how to refer to that specific comment and change the vote column. I'm sure there is a django way to do it, but can't seem to find it in the docs. Thanks!

Upvotes: 1

Views: 133

Answers (1)

Dorian Dore
Dorian Dore

Reputation: 962

Try passing the comment's id value as a hidden input field, then use the filter method to find the comment and operate on it in the view. Example:

{% for c in comments %}
            <br>
            <a href="{% url 'user_profile' c.user_comments %}"> {{c.name}} </a> <br><br>
            <p> {{c.comment}} </p> </br>
            {% if user.is_authenticated %}
            <form method="POST">
                {% csrf_token %}
                    <input class='small' type='submit' value='+1' name='commentup'>
                    <input class='small' type='submit' value='-1' name='commentdown'>
                    <input type='hidden' value='{{c.id}}' name='commentID'>
            </form>
            <p> Score: {{c.vote}} </p>
            {% endif %}
        {% endfor %}

And then in the view:

def view_post(request, question_id):
    tpost = get_object_or_404(Post, pk=question_id)
    comment = tpost.comments_set.filter(pk=request.POST['commentID'])[0]
    [...]

Also, you need to save a object after making changes to it. So in the view, you can do:

if request.POST.get('commentup', ''): self.vote += 1
if request.POST.get('commentdown', ''): self.vote -= 1
self.save()

Upvotes: 1

Related Questions