[Fixed]-Error when returning a HttpResponse from a Django Helper Function

1👍

You can separate your view and use JsonResponse directly, and you should return a response if your test fail, this might be your issue since they told you that it returned None instead. I believe you should write something like this

from django.http import JsonResponse

def get_industry(request):
    if request.is_ajax() and request.method == 'GET':
        if request.GET.get('typeis') =='industry':
            print('Now loading industries')
            sectorid = int(request.GET.get('sector_is'))
            sector = models.SecSectorMaster.objects.filter(pk=sectorid).order_by('sectorname')
            industries = models.SecIndustryMaster.objects.filter(sectorid=sector).order_by('industryname')
            industry_dict = {}
            for this_i in industries:
                industry_dict[this_i.industryid] = this_i.industryname
            return JsonResponse(industry_dict)
        return JsonResponse({'industry': None})
👤Alejo

Leave a comment