Access dynamic session variable name in template

70 views
Skip to first unread message

jorr...@gmail.com

unread,
Jul 2, 2015, 2:09:59 PM7/2/15
to django...@googlegroups.com
Hi everyone,

I am trying to keep track of who has voted on a comment by setting session variables like 'commentvote1', 'commentvote2', etc to True. These variable names are dynamically generated based on the id of the comment. How can I access these dynamic variables in my template? I've tried this but it doesn't seem to work:

views.py

def comment_upvote(request, comment_id):
    comment
= get_object_or_404(Comment.objects.filter(id__exact=comment_id,approved=True))
   
if 'commentvote' + str(comment.id) not in request.session:    
        comment
.upvotes = comment.upvotes + 1
        comment
.votes = comment.votes + 1
        comment
.save()
        request
.session['commentvote' + str(comment.id)] = True
   
else:
        messages
.error(request, 'You have already voted on this comment.')
   
return redirect('comments')        

The logic in the view is confirmed to work - It won't let you vote on the same comment twice.

template

{% with 'commentvote'|add:comment.id as voted %}
{% if not request.session.voted %}
<button type="button" class="btn btn-success" aria-label="Vote Up"
onclick
="javascript:document.location.href='{% url 'comment_upvote' comment.id %}';">
   
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
</button>
<button type="button" class="btn btn-danger" aria-label="Vote Down"
onclick="javascript:document.location.href='{% url 'comment_downvote' comment.id %}';">
     <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></
span>
</button>
{% endif %}
{% endwith %}

Any help would be appreciated!

Daniel Roseman

unread,
Jul 2, 2015, 2:41:48 PM7/2/15
to django...@googlegroups.com
I think you have the wrong data structure. Instead of storing all the comment votes as separate keys in the session,  consider storing them in a single "commentvotes" dictionary:

    votes = request.session.setdefault('commentvotes', {})
    if comment.id not in votes:
        ...
        votes[comment.id] = True
    else:
        ...

and the template:

    {% if comment.id not in request.session.votes %}

--
Daniel.

jorr...@gmail.com

unread,
Jul 2, 2015, 8:39:57 PM7/2/15
to django...@googlegroups.com
This doesn't seem to work :(

def comment_upvote(request, comment_id):
    comment
= get_object_or_404(Comment.objects.filter(id__exact=comment_id,approved=True))

    votes
= request.session.setdefault('commentvotes', {})
   
if comment.id not in votes:
   
        comment
.upvotes = comment.upvotes + 1

        comment
.votes = comment.votes + 1
        comment
.save()

        votes
[comment.id] = True
   
else:

        messages
.error(request, 'You have already voted on this comment.')
   
return redirect('comments')        

                            {% if comment.id not in request.session.commentvotes %}

                           
<button type="button" class="btn btn-success" aria-label="Vote Up"

                            data
-toggle="tooltip" data-placement="right" title="Vote up"

                            onclick
="javascript:document.location.href='{% url 'comment_upvote' comment.id %}';">
                               
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
                           
<
/button>

                            <button type="button" class="btn btn-danger" aria-label="Vote Down"
                            data-toggle="tooltip" data-placement="right" title="Vote down"

                            onclick="javascript:document.location.href='{% url 'comment_downvote' comment.id %}';">
                                <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></
span>
                           
</button>
                           
{% endif %}

With this code the first time that someone loads the comments page (fresh session) no buttons show up at all (I guess because the session key is not set?). And it also doesn't prevent voting on the same comment twice.
Reply all
Reply to author
Forward
0 new messages