S
S
Sergey Nizhny Novgorod2016-07-07 17:21:39
Django
Sergey Nizhny Novgorod, 2016-07-07 17:21:39

Django/ajax how to display new data on current page without page refresh?

Hello.
I make a function:
A person clicks on a button (link), the number of likes increases by one. Now I made this function through Ajax, but the problem is that the number of likes is increasing, but this can only be seen after the page is refreshed. How to make the update go instantly?
Button/Link:

<a id="upvideolike" class="article_social_thumb" href="">{{ videoitem.get_plus }}</a> #get_plus - число лайков.

js code:
<script type="text/javascript">

    $('#upvideolike').click(function(e){
        e.preventDefault();

        $.ajax({
            url: '{% url "upvideolike" videoitem.id %}',
            data:{

            },
            sucess:function(){

            }
        })
    });

</script>

Add plus view:
def upvideolike(request, add_id):

    if request.user.is_authenticated():
        video_item = Video.objects.get(id = add_id)
        user_tags = User.objects.filter(users_video_main = add_id)
        current_user = request.user
        if current_user not in user_tags:
            try:
                video_item = Video.objects.get(id = add_id)
                video_item.thumbnumber +=1
                video_item.likedone.add(current_user)
                video_item.save()
                return HttpResponse()
            except ObjectDoesNotExist:
                return HttpResponse()
        else:
            return HttpResponse()
    else:
        return HttpResponse()

Page formation view:
def video(request, video_id):
    videoitem = Video.objects.get(id = video_id)

    context = {
        "videoitem" : videoitem,
    }
    return render(request, 'faceset/videopage.html', context)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-07-07
@Terras

https://docs.djangoproject.com/en/1.9/ref/request-...

if request.is_ajax(): 
   JsonResponse(
      # data, encoder=DjangoJSONEncoder, 
      safe=False, 
      # json_dumps_params=None, 
    {'like_counter': 42})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question