Package: django-star-ratings
Python 3 compatible ratings for Django.Add ratings to any Django model with a template tag.See fulldocumentation.Built by Wildfish.https://wildfish.comRequirementsPython 3.6+.Django 2.2+InstallationInstall from PyPI:pip install django-star-ratingsaddstar_ratingstoINSTALLED_APPS:INSTALLED_APPS = (
    ...
    'star_ratings'
)sync your database:python manage.py migrateadd the following to your urls.py:path('ratings/', include('star_ratings.urls', namespace='ratings')),Make sure'django.core.context_processors.request',is inTEMPLATE_CONTEXT_PROCESSORS.UsageAdd the following javascript and stylesheet to your template{% load static %}
<html>
...
<link rel="stylesheet" href="{% static 'star-ratings/css/star-ratings.css' %}">
<script type="text/javascript" src="{% static 'star-ratings/js/dist/star-ratings.min.js' %}"></script>
...
</html>To enable ratings for a model add the following tag in your template{% load ratings %}
<html>
...
{% ratings object %}
...
</html>Template tagsThe template tag takes four arguments:icon_height: defaults toSTAR_RATINGS_STAR_HEIGHTicon_width: defaults toSTAR_RATINGS_STAR_WIDTHread_only: overrides theeditablebehaviour to make the widget read onlytemplate_name: overrides the tempalte to use for the widgetSettingsTo prohibit users from altering their ratings setSTAR_RATINGS_RERATE = Falsein settings.pyTo allow users to delete a rating by selecting the same score again, setSTAR_RATINGS_RERATE_SAME_DELETE = Truein settings.py, note
thatSTAR_RATINGS_RERATEmust be True if this is set.To allow uses to delete a rating via a clear button, setSTAR_RATINGS_CLEARABLE = True`in settings.py. This can be used
with or without STAR_RATINGS_RERATE.To change the number of rating stars, setSTAR_RATINGS_RANGE(defaults to 5)To enable anonymous rating setSTAR_RATINGS_ANONYMOUS = True.Please note thatSTAR_RATINGS_RERATE,STAR_RATINGS_RERATE_SAME_DELETEandSTAR_RATINGS_CLEARABLEwill have no affect when anonymous rating is enabled.Anonymous RatingIf anonymous rating is enabled only the ip address for the rater will be stored (even if the user is logged in).
When a user rates an object a preexisting object will not be searched for, instead a new rating object will be createdIf this value is changed your lookups will return different results!To control the default size of stars in pixels set the values ofSTAR_RATINGS_STAR_HEIGHTandSTAR_RATINGS_STAR_WIDTH. By defaultSTAR_RATINGS_STAR_WIDTHis the same asSTAR_RATINGS_STAR_HEIGHTandSTAR_RATINGS_STAR_HEIGHTdefaults to 32.Changing the star graphicsTo change the star graphic, add a sprite sheet to/static/star-ratings/images/stars.pngwith the states aligned
horizontally. The stars should be laid out in three states: full, empty
and active.You can also setSTAR_RATINGS_STAR_SPRITEto the location of your sprite sheet.Customize widget templateYou can customize ratings widget by creatingstar_ratings/widget.html. For example :{% extends "star_ratings/widget_base.html" %}
{% block rating_detail %}
Whatever you want
{% endblock %}Seestar_ratings/widget_base.htmlfor other blocks to be extended.Ordering by ratingsThe easiest way to order by ratings is to add aGenericRelationto
theRatingmodel from your model:from django.contrib.contenttypes.fields import GenericRelation
from star_ratings.models import Rating

class Foo(models.Model):
    bar = models.CharField(max_length=100)
    ratings = GenericRelation(Rating, related_query_name='foos')

Foo.objects.filter(ratings__isnull=False).order_by('ratings__average')Custom Rating ModelIn some cases you may need to create your own rating model. This is possible
by settingSTAR_RATING_RATING_MODELin your settings file. This can be useful
to add additional fields or methods to the model. This is very similar to the how
django handles swapping the user model
(see [https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#substituting-a-custom-user-model]).For easeAbstractBaseRatingis supplied. For example if you wanted to add the
fieldfooto the rating model you would need to crate your rating model
extendingAbstractBaseRating:./myapp/models.py

class MyRating(AbstractBaseRating):
   foo = models.TextField()And add the setting to the setting file:./settings.py

...
STAR_RATINGS_RATING_MODEL = 'myapp.MyRating'
...NOTE:If you are using a custom rating model there is an issue with how django
migration handles dependency orders. In order to create your initial migration you
will need to comment out theSTAR_RATINGS_RATING_MODELsetting and runmakemigrations. After this initial migration you will be able to add the setting
back in and runmigrateandmakemigrationswithout issue.Changing thepktype (Requires django >= 1.10)One use case for changing the rating model would be to change the pk type of the
related object. By default we assume the pk of the rated object will be a
positive integer field which is fine for most uses, if this isn’t though you will
need to override theobject_idfield on the rating model as well as set
STAR_RATINGS_OBJECT_ID_PATTERN to a reasonable value for your new pk field. As
of django 1.10 you can now hide fields form parent abstract models, so to change
theobject_id``toa ``CharFieldyou can do something like:class MyRating(AbstractBaseRating):
   object_id = models.CharField(max_length=10)And add the setting to the setting file:./settings.py

...
STAR_RATINGS_OBJECT_ID_PATTERN = '[a-z0-9]{32}'
...EventsSome events are dispatched from the javascript when an object is raised. Each
event that ias dispatched has adetailsproperty that contains information
about the object and the rating.rate-successDispatched after the user has rated an object and the display has been updated.The event details contains{
    sender: ... // The star DOM object that was clicked
    rating: {
        average: ... // Float giving the updated average of the rating
        count: ... // Integer giving the total number of ratings
        percentage: ... // Float giving the percentage rating
        total: ... // Integer giving the sum of all ratings
        user_rating: ... // Integer giving the rating by the user
}rate-failedDispatched after the user has rated an object but the server responds with an error.The event details contains{
    sender: ... // The star DOM object that was clicked
    error: ... // String giving the error message from the server
}Running testsTo run the test use:$> ./runtests.pyReleasingTravis is setup to push releases to pypi automatically on tags, to do a release:Up version number.Update release notes.Push dev.Merge develop into master.Tag with new version number.Push tags.