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

后端 未结 1 1343
耶瑟儿~
耶瑟儿~ 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

    and save it to another file. Lets say,

    rating.html

     
    {{ object.rating_vote.get_rating }} {{ object.rating_vote.get_rating }}

    Now in your page, it will be,

    your.html

    ............
    
    {% include 'rating.html' %}
    ............

    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)
提交回复
热议问题