Refresh a div to shows new rating in Django using JQuery and AJAX

后端 未结 1 1342
耶瑟儿~
耶瑟儿~ 2021-01-07 00:13

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 so

相关标签:
1条回答
  • 2021-01-07 00:30

    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/");
        },
    });
    ...........
    
    0 讨论(0)
提交回复
热议问题