django抛出异常然后在axios里处理
1.views.py代码
@csrf_exempt
def get_selectdata_by_sql(request):
current = request.GET.get("current", 1)
size = request.GET.get("size", 0)
limit_size=50
try:
res_list = []
request_dict = json.loads(request.body)
# print(request_dict)
runsql = request_dict.get("sql").replace(";", "") + ' limit '+str(limit_size)
print('runsql=', runsql)
# 有值才处理
with connection.cursor() as cursor:
cursor.execute(runsql)
col_names = [desc[0] for desc in cursor.description]
print('col_names=', col_names)
row = cursor.fetchall()
rowList = []
for list in row:
tMap = dict(zip(col_names, list))
rowList.append(tMap)
# 总数
total = rowList.__len__()
if current and size:
end = int(current) * int(size)
start = (int(current) - 1) * int(size)
rowList = rowList[start:end]
for user in rowList:
res_list.append(user)
res = {
"columns": col_names,
"rows": res_list,
"total": total,
"result": True
}
return JsonResponse(res, safe=False)
except Exception as e:
#捕获异常并返回错误信息
return JsonResponse({'error': str(e)}, status=400)
2.vue处理
3.效果展现