Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve AssertionError: .accepted_renderer not set on Response in django and ajax

Tags:

ajax

django

While I am calling Django url in ajax, getting below error

AssertionError: .accepted_renderer not set on Response.

This is my code:

function download(){

    $.ajax({
        url: "/mdm/exam_app/get_assessment_count/",
        dataType: 'json',
        data:{
        },
        type:'GET',
        success: function (data) {
          alert("inside the success method");
    },
        error: function(){
          console.log("error");
          }        
      });
}
like image 217
navnd Avatar asked Mar 29 '19 11:03

navnd


3 Answers

If you're using a function based view, then this issue usually means you forgot to add the @api_view and the @renderer_classes decorator to your view.

Example:

from rest_framework.decorators import api_view, renderer_classes
from rest_framework.renderers import JSONRenderer, TemplateHTMLRenderer

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def get_assessment_count(request):
    [...]
    data = {'count': queryset.count()}
    return Response(data, template_name='assessments.html')
like image 135
Hybrid Avatar answered Oct 07 '22 02:10

Hybrid


In addition to the accepted answer by @DavidLam it could also be that an error was thrown in your view/handler4xx/handler5xx and you've not caught it appropriately.

like image 28
Marc Avatar answered Oct 07 '22 00:10

Marc


If anyone have this issue with Django Rest Framework while using Response in a class, don't forget to use APIView instead of View.

like image 8
B. Okba Avatar answered Oct 07 '22 01:10

B. Okba