Simple Django application for adding Youtube like up and down voting.
Thisdjango-updown-ratings
is forked fromdjango-updown
to support the newest django version.
pip install django-updown-ratings
Add "updown"
to your INSTALLED_APPS
. Then just add a RatingField
to your existing model:
from django.db import models
from updown.fields import RatingField
class Post(models.Model):
# ...other fields...
rating = RatingField()
You can also allow the user to change his vote:
class Post(models.Model):
# ...other fields...
rating = RatingField(can_change_vote=True)
Now you can write your own view to submit ratings or use the predefined:
from updown.views import AddRatingFromModel
urlpatterns = [
....
path('<int:object_id>/rate/<str:score>', AddRatingFromModel(), {
'app_label': 'blogapp',
'model': 'Post',
'field_name': 'rating'
}, name='post_rating'),
]
To submit a vote just go to post/<id>/rate/(1|-1)
. If you allowed users to
change they're vote, they can do it with the same url.
You can directly use this properties in the template, e.g:
{{ post.rating.likes }}
{{ post.rating.dislikes }}
{{ post.rating.get_difference }}
{{ post.rating.get_quotient }}
Properties:
.likes
show total likes.dislikes
show total dislikes.get_difference
get diff between likes and dislikes (likes - dislikes
)..get_quotient
get quotient between likes and dislikes (likes / max(dislikes)
).