Django只能将str(而不是“ list”)连接到str

时间:2019-03-27 02:20:54

标签: django

Django只能将str(而不是“ list”)连接到str 错误消息.......

我有一些这样的代码:

function form_submit() {
    var arr_category = new Array();
    var arr_lawyer = new Array();
    var data = new Object();

    $('input[name^="category_idx"]').each(function() {
        arr_category.push($(this).val());
    });

    $('input[name^="lawyer_idx"]').each(function() {
        arr_lawyer.push($(this).val());
    });

    console.log("arr_category=="+arr_category);
    console.log("arr_lawyer=="+arr_lawyer);

    if (confirm('edit??') == true) {
        data.arr_category = arr_category;
        data.arr_lawyer = arr_lawyer;
        call_ajax('/admin/lawyer/recommend_add', data);
        //alert("arr_lawyer=="+arr_lawyer);
    }
}

我在jquery上做得好吗? 看console.log

arr_category==1,2,3,4,5,6,7,8,9
arr_lawyer==64,37,57,58,130,62,38,51,110

admin_view.py

@csrf_exempt
def recommend_add(request):

    print("TEST BANG VALUE------------")
    if request.is_ajax() and request.method == "POST":

        arr_category = request.GET.getlist('arr_category[]')
        print("arr_category------------" + arr_category)

    code = 0
    msg = "TEST."

    data = json.dumps({
        'code': code,
        'msg': msg,
        #'retURL': retURL

    })
    return HttpResponse(data, content_type='application/json')

我要打印。 错误信息 TypeError:只能将str(而不是“ list”)连接到str

我该怎么做?

2 个答案:

答案 0 :(得分:0)

在您的打印代码中尝试

print("arr_category------------:", arr_category)

答案 1 :(得分:0)

如果要在jQuery的ajax调用中返回python解释器错误消息,则可以使用以下语法。

def recommend_add(request):
 print("TEST BANG VALUE------------")
 if request.is_ajax() and request.method == "POST":
   try:
     arr_category = request.GET.getlist('arr_category[]')
     print("arr_category------------" + arr_category)
     code = 0
     msg = "Success"
   except Exception as e:
     code = '' # anything you want as per your internal logic in case of error.
     msg = str(e)

 data = json.dumps({
    'code': code,
    'msg': msg,
    #'retURL': retURL

 })
 return HttpResponse(data, content_type='application/json')

请忽略我的缩进。