While working on this recently, I thought that I would suggest a solution for the mix. Firstly, I use RateIt, which, it seemed to me, is very easy to set up and quite intuitve to use (add the RateIt *.js and .*css files to the base.html template):
http://www.radioactivethinking.com/rateit/example/example.htm
Here are the key parts of my solution:
urls.py
url(r'^object/rate/$', RateMyObjectView.as_view(), name='rate_my_object_view'),
my_template.html
<div class="rateit" data-rateit-resetable="false">Rate it!</div>
ajax.js
$('.rateit').bind('click', function(e) { e.preventDefault(); var ri = $(this); var value = ri.rateit('value'); var object_id = ri.data('object_id'); $.ajax({ url: '/object/rate/?xhr', data: { object_id: object_id, value: value }, type: 'post', success: function(data, response) { console.log("ajax call succeeded!"); }, error: function(data, response) { console.log("ajax call failed!"); } }); });
Some view bits belong to James Bennett (setting xhr , for example):
http://www.b-list.org/weblog/2006/jul/31/django-tips-simple-ajax-example-part-1/
views.py
from django.views.generic.base import View from .models import MyObject class RateMyObjectView(View): def post(self, request): my_object = MyObject.objects.all().last() xhr = 'xhr' in request.GET star_value = request.POST.get('value', '') my_object.score = star_value my_object.save() response_data = { 'message': 'value of star rating:', 'value': star_value } if xhr and star_value: response_data.update({'success': True}) else: response_data.update({'success': False}) if xhr: return HttpResponse(json.dumps(response_data), content_type="application/json") return render_to_response(self.template_name, response_data)
models.py
from django.db import models class MyObject(models.Model) score = models.FloatField(max_length=1, default=0)
Keep in mind that this is a naive solution and simply replaces the current star score in the last item in the list of objects. This is not ideal, since it would be better to store the ratings as their own model and object reference. That you could store them and perform calculations as averages, etc. I am working on it now and will update this answer when done.