Django - 即使HttpResponse返回,也没有返回HttpResponse对象

时间:2015-06-24 06:30:04

标签: django django-views

我得到的错误:

视图report.views.csv_gen_universal未返回HttpResponse对象。

def csv_gen_universal(req):
    if req.POST['csvinputid']:
        # Create the HttpResponse object with the appropriate CSV header.
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] ='attachment;filename="M_Wall_report.csv"'
        writer = csv.writer(response)
        content = str(req.POST['csvinputid']).split("^~^")
        for m in content :
            p = m.split("*~*")
            writer.writerow(p)
        return response
    else:
        HttpResponse("Wrong Place")

1 个答案:

答案 0 :(得分:3)

您忘了在else语句中添加return。你的代码应该是

def csv_gen_universal(req):
    if req.POST['csvinputid']:
        # Create the HttpResponse object with the appropriate CSV header.
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] ='attachment;filename="M_Wall_report.csv"'
        writer = csv.writer(response)
        content = str(req.POST['csvinputid']).split("^~^")
        for m in content :
            p = m.split("*~*")
            writer.writerow(p)
        return response
    else:
        return HttpResponse("Wrong Place")