我试图以Json格式将数据传递给模板。JsonResponse返回一个错误
TypeError:类型方法的对象不是JSON可序列化的
下面是导致错误的代码视图:
if request.method == 'POST':
... SOME CODE HERE ....
elif request.method == 'GET' and request.is_ajax():
df = pd.read_csv(project.base_file, encoding='ISO-8859-1')
cols = df.keys
return JsonResponse({'features': cols, }, status=200)
else:
form = mForm(project_id=pk)
Json数据应该在以下Ajax代码中处理:
$(document).ready(function(){
var id_number =$("#projectID").text();
$("#btnSelect").click(function(){
$.ajax({
url: '',
method: 'GET',
data: {
project_id: id_number,
},
success: function(response){
$("#id_features").text(response.features)
}
});
});
});
发布于 2020-08-15 07:01:49
问题在于:
df = pd.read_csv(project.base_file, encoding='ISO-8859-1')
cols = df.keys
^
因为keys
是一种方法,所以不能将方法序列化为JSON(这就是错误标题,对吗?)由于您希望将df
值作为序列化的JSON,所以必须调用该方法来实现它
但试着
cols=df.keys()
https://stackoverflow.com/questions/63427371
复制相似问题