shellfly / django-vote

Simple vote for django
http://django-vote.readthedocs.io
Apache License 2.0
158 stars 49 forks source link

How to see what user voted? #48

Closed killerbarney closed 7 years ago

killerbarney commented 7 years ago

I'm not sure if this is an "issue", but how are we supposed to see what the user voted? Also, there's very little documentation on template tags we can use. How do we figure that out?

shellfly commented 7 years ago

You can get all voted instances by a user when you know the model like this Review.votes.all(user_id). There is no method to get all instance by different models yet.

For template, there is a simple template tag to check if a user has voted a instance templatetag, I'm not sure if it can fit your needs.

bguggs commented 7 years ago

Just experimented with this. You can do it.

Using the API:

<model>.votes.exists(user, action=0) #Did user vote up?
<model>.votes.exists(user, action=1) #Did user vote down?

If both return false, then the user has not voted.

In the template:

{% load vote %}

{% vote_exists <model> request.user 0 as vote_up %}
{% vote_exists <model> request.user 1 as vote_down %}

{{ vote_up }} <!-- True if user has upvoted --!>
{{ vote_down }} <!-- True if user has downvoted --!>