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.效果展现

 

posted @   slnngk  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2022-11-04 orchestrator安装部署(raft版本)
2020-11-04 安装grid时报INS-40404错误
点击右上角即可分享
微信分享提示