[Answered ]-How to refresh parts of a page in Django + jQuery?

2👍

✅

Your count view should return Count value and not redirect:

def count(request, pk):
    c = Count.objects.filter(id=1).update(num=pk)
    return new HttpResponse(pk)

Your AJAX call should update span‘s value based on the response from the server:

$.ajax({
    url: '{% url 'alpha:count' 1 %}',
    success: function(data) {
        $('#num').text(data);
    }
})

Additionally I’d extract AJAX into a function, eg:

function updateCount(value){
    $.ajax({
        url: '/count/' + value,
        success: function(data) {
            $('#num').text(data);
        }
    })
}

and then reuse it twice in your HTML:

<a href="#" onclick="updateCount(1);">1</a>
<a href="#" onclick="updateCount(2);">2</a>

Leave a comment