[Fixed]-Cant get ajax response to place nice with python code

1πŸ‘

βœ…

the code here is never reached:

            else:   
              x_json['success'] = 'workorder_not_found' 
              results.append(x_json) 

because if if wo_data.exists(): is not true, then for x in wo_data: would never have any iterations in the first place.

Try:

def get_work(request): 
    if request.is_ajax(): 
        q = request.GET.get('workorder_id', '') 
        wo_data = Dim_work_order.objects.filter(base_id__icontains = q )[:1] 
        results = [] 
        if wo_data.exists(): 
            for x in wo_data: 
                x_json = {} 
                x_json['success'] = 'success' 
                x_json['work_o'] = x.base_id 
                x_json['customer_name'] = x.name 
                x_json['part_number'] = x.part_id 
                x_json['part_rev'] = x.part_rev 
                x_json['purch_order'] = x.customer_po_ref 
                results.append(x_json) 
        else:
            results.append({'success': 'workorder_not_found'}) 

        data = json.dumps(results) 
        mimetype = 'application/json' 
        return HttpResponse(data, mimetype) 
πŸ‘€ben432rew

Leave a comment