당신은 아마 서버에 연결하고 클릭을 기록하는 Ajax 호출을 구현하고자합니다. 예를 들어, jQuery를 사용하여, 당신은 할 수있는이 클릭이 발생할 때마다 호출 된 함수를 JS :
function incrementPopCounter(pop_id) {
$.ajax({type: 'POST',
dataType: 'json',
url: '/pop/increment',
data: 'id=' + pop_id,
success: function(data) {
if(data.result == 'OK') {
// handle success
} else {
// handle failure
}
}
});
return false;
}
이 다음과 같을 수 처리하는 장고보기 :
이
from django.utils import simplejson
...
def increment_pop(request):
if request.is_ajax():
if 'pop_id' in request.POST and request.POST['pop_id']:
try:
pop = Bookmark.objects.get(pk=request.POST['pop_id'])
except Bookmark.DoesNotExist:
return HttpResponse(simplejson.dumps({'result': 'No bookmark by that id found.'}),
mimetype='application/json')
pop.popularity = F('popularity') + 1
pop.save()
return HttpResponse(simplejson.dumps({'result': 'OK'}),
mimetype='application/json')
else:
return HttpResponse(simplejson.dumps({'result': 'Unable to identify the requested bookmark.'}),
mimetype='application/json')
else:
return HttpResponseBadRequest()