Y
Y
YraganTron2017-03-12 22:03:04
Django
YraganTron, 2017-03-12 22:03:04

How to handle ajax request in django cbv?

I am rewriting the project into classes. I need to process an ajax request, I don’t understand how to do it at all, whether I need to write a mixin or create a separate class. Here is the code that was written in functions.

if request.is_ajax():
        if request.GET.get('value'):
            x = int(request.GET.get('value'))
            thread_ajax = Thread.objects.filter(board=Board.objects.get(board_shortcut=
                                                                        name_board)).order_by('-thread_score')
            if x + 5 > len(thread_ajax):
                thread_ajax = thread_ajax[x:len(thread_ajax)]
            else:
                thread_ajax = thread_ajax[x:x + 5]
            comment_ajax = []
            for x in thread_ajax:
                if Comment.objects.filter(thread=x).count() > 3:
                    section = Comment.objects.filter(thread=x).count() - 3
                else:
                    section = 0
                if Comment.objects.filter(thread=x)[section:].count() != 0:
                    Not_QuerySet = list(Comment.objects.filter(thread=x)[section:])
                    comment_ajax.extend(Not_QuerySet)
            all = list(thread_ajax) + list(comment_ajax)
            data = serializers.serialize('json', all)

            return HttpResponse(data, content_type='application/json')

In Google I found the current ajax forms, but to be honest, this did not bring me one step closer to the goal.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-03-13
@YraganTron

Actually, it is not clear what the question is.
There is https://docs.djangoproject.com/en/1.10/ref/request...
There is https://docs.djangoproject.com/en/1.10/topics/clas...
If necessary "include" the answer in an already existing class, then the first
If a separate view, then - the second
in a try-except is still worth wrapping
x = int(request.GET.get('value'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question