Code Monkey home page Code Monkey logo

django-star-ratings's Introduction

django-star-ratings

Build Status codecov.io

Python 3 compatible ratings for Django.

Add ratings to any Django model with a template tag.

Installation

Install from PyPI:

pip install django-star-ratings

add star_ratings to INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'star_ratings'
)

sync your database:

python manage.py migrate

add the following to your urls.py:

url(r'^ratings/', include('star_ratings.urls', namespace='ratings', app_name='ratings')),

Make sure 'django.core.context_processors.request', is in TEMPLATE_CONTEXT_PROCESSORS.

Usage

Add 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 tags

The template tag takes two arguments:

  • icon_height: defaults to 32
  • icon_width: defaults to 32

Settings

To prohibit users from altering their ratings set STAR_RATINGS_RERATE = False in settings.py

To change the number of rating stars, set STAR_RATINGS_RANGE (defaults to 5)

Changing the star graphics

To change the star graphic, add a sprite sheet to /static/star-ratings/images/stars.png with the states aligned horizontally. The stars should be laid out in three states: full, empty and active.

Customize widget template

You can customize ratings widget by creating star_ratings/widget.html. For example :

{% extends "star_ratings/widget_base.html" %}
{% block ratings_detail %}
Whatever you want
{% endblock %}

See star_ratings/widget_base.html for other blocks to be extended.

Ordering by ratings

The easiest way to order by ratings is to add a GenericRelation to the Rating model from your model:

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')

Running tests

To run the test use:

$> ./runtests.py

django-star-ratings's People

Contributors

bhy avatar fuzzpedal avatar jneves avatar omegadroid avatar rolo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.