开发者

Django-ratings and Jquery integration?

How do I make django-ratings work with Jquery?

What I'm trying to do is allow the user to select how ever man开发者_如何学编程y stars they want to give the product, and to have the corresponding rating processed asynchronously. I realize this is probably basic AJAX, and I apologize if this is a stupid question.

Thank you in advanced.


I'm not sure if I understand your question, but is your question from the javascript side or from the model side? From javascript, I used something like this

STARS_ELEMENT.stars({
    callback: function(ui, type, value){
        $.post('URL_ADDRESS', {rate: value},
            function(data){
                STARS_ELEMENT.stars("select",data);
            });                         
    }

Then in my view, I would have a function that captures that request and do this

p = Product.objects.get(id=product_id)
p.rating.add(score=int(request.POST['rate']), user=request.user, ip_address=request.META['REMOTE_ADDR'])
p.save()

And use the request to send me back the most updated rate value. Is that what you were looking for?


Usually converting to AJAX is as simple as replacing:

return HttpResponse(....)

with:

if request.is_ajax():
    return json_response
else:
    return HttpResponse(....)

or even requiring AJAX:

if request.is_ajax():
    return json_response
else:
    return HttpResponseForbidden('Only AJAX please!')

But, from what I see here there are some complext patterns for generating the actual response. So, you can either try working with that in jQuery AJAX response handler, by parsing the response - or you can write a decorator for AddRatingView.call that parses the response on the Python side and returns jQuery-friendly JSON.

I assume here that you don't want to mess a lot with the existing django-rating code, e.g. to preserve the option to upgrade it.


You are right,its a basic Ajax, but you might have a complex algorithm to figure out the number of stars as

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜