如何解决AssertionError:未在Django和Ajax中的Response上设置.accepted_renderer [英] How to resolve AssertionError: .accepted_renderer not set on Response in django and ajax
本文介绍了如何解决AssertionError:未在Django和Ajax中的Response上设置.accepted_renderer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在ajax中调用Django url时,出现以下错误
While I am calling Django url in ajax, getting below error
AssertionError:未在Response上设置.accepted_renderer.
AssertionError: .accepted_renderer not set on Response.
这是我的代码:
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");
}
});
}
推荐答案
如果您使用的是基于函数的视图,那么此问题通常意味着您忘记添加 @ renderer_classes 装饰器.
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.
示例:
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')
这篇关于如何解决AssertionError:未在Django和Ajax中的Response上设置.accepted_renderer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文