Multiple ajax requests kill django server

190 views Asked by At

I have a single view which scrolls through user data. There are next and previous buttons to scroll. When user presses next, ajax sends the user id to the django view and displays the data. If user clicks the next button two or three times consecutively (which they usually do), the calls get aborted and server is killed.

$("#new").click(function() {
     $.ajax({
        type:'POST',
        url:'/new/',
        data:{
               csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
        },
        success:searchSuccess,
        dataType: 'html'
    });
});

function searchSuccess(data, textStatus, jqXHR)
    {
        $('#myForm').html(data);
     }

This is the view.

def master_detail_next(request):
    def decrement_voucher_id(form_id):
        voucher_id = str(int(form_id) - 1).zfill(4)
        return voucher_id

    if request.method == 'POST':
        form_id = request.POST['voucher_id']
        voucher_id = decrement_voucher_id(form_id)
        voucher_id = get_decremented_voucher_id(voucher_id)
    else:
        voucher_id = ''

    # Inline forms
    author = TmpPlInvoice.objects.get(voucher_id=voucher_id)
    author_form = TmpForm(instance=author)

    BookFormSet = inlineformset_factory(TmpPlInvoice, TmpPlInvoicedet,
                                    exclude=('emp_id', 'voucher', 'lineitem', 'id',),
                                    form=TmpFormDetForm, )
    formset = BookFormSet(instance=author)
    totalform = TmpFormTotal(instance=author)
    postform = CheckPostedForm(instance=author, posted=author.posted)
    return render(request, 'form.html', {'form': author_form, 'formset': formset, 'formtotal': totalform, 'postform': postform})

How can i avoid that? What is that i am doing wrong?

Ajax calls

0

There are 0 answers