Skip to content Skip to sidebar Skip to footer

Refresh A Div To Shows New Rating In Django Using JQuery And AJAX

I’m new to django and can’t find the way to refresh only the div and that the div shows me current rating with stars. My idea is that user can see average rating and rate somet

Solution 1:

First, copy the code inside the <div id="rating"></div> and save it to another file. Lets say,

rating.html

 <div class="movierating" id="o_{{ object.id }}">
    <span style="display:none;">{{ object.rating_vote.get_rating }}</span>
    {{ object.rating_vote.get_rating }}
</div>

Now in your page, it will be,

your.html

............
<div id="rating">
    {% include 'rating.html' %}
</div>
............

Then, create new view and new url for rating.html

urls.py

url(r'^rating/$', 'rating', name='rating'),

views.py

def rating(request):
    //rating object here

    return render(request, 'rating.html', {
        //call rating object variable here
    })

Finally, in your ajax

.........
$.ajax({
    url: vote_url,
    success: function(){
        alert('Vote successful!);
        $("#rating").load("/app_name/rating/");
    },
});
...........

Post a Comment for "Refresh A Div To Shows New Rating In Django Using JQuery And AJAX"