[Fixed]-Django, I can't return httpresponse data with ajax on admin change_list view

1👍

admin.py

        if request.method == 'POST' and request.is_ajax():
        form = PinForm(request.POST)
        if form.is_valid():
            extra_context['pinform'] = form
            return HttpResponse(json.dumps({'balance': form.cleaned_data['pin']}), content_type='application/json')

    else:
        extra_context['pinform'] = PinForm()
        return super(TRANSACTION_DISPLAY, self).changelist_view(request, extra_context=extra_context)

Change_list.js

$.ajax({
       url : window.location.href, // the endpoint,commonly same url
       type : "POST", // http method
       crossDomain: false,
       data : { csrfmiddlewaretoken : csrftoken,
                pin : pin
         }, // data sent with the post request

         // handle a successful response
       success : function(data) {
            $('#myModal .close').click();
            $('#balanceModal').modal('show');
            $('#balance').addClass('lead').text(data['balance']);
       },

         // handle a non-successful response
       error : function(xhr,errmsg,err) {
         console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
         }
         });
    });

Leave a comment