tigerJK
tigerJK

Reputation: 35

Django can only concatenate str (not "list") to str

Django can only concatenate str (not "list") to str Error Messages.......

I have some code like this:

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);
    }
}

Am I doing well in jquery? look at 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')

I want to print. error message TypeError: can only concatenate str (not "list") to str

How can I do that?

Upvotes: 0

Views: 968

Answers (2)

SwapnilBhate
SwapnilBhate

Reputation: 274

In case if you want to return a python interpreter error message to jquery's ajax call then you can use the below syntax.

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')

Please ignore my indentation.

Upvotes: 1

Dimitris Kougioumtzis
Dimitris Kougioumtzis

Reputation: 2439

in your print code try this

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

Upvotes: 0

Related Questions