0

异常类型:ValueError 异常值:

视图 canvas.views.pdfDirectory 没有返回 HttpResponse 对象。

异常位置:/usr/local/lib/python2.6/dist-packages/Django-1.4.3-py2.6.egg/django/core/handlers/base.py in get_response,第 129 行

import os
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import cm, mm, inch, pica

def pdfDirectory(imageDirectory, outputPDFName = 'ima.pdf'):
    dirim = str(imageDirectory)
    output = str(outputPDFName)
    width, height = letter
    height, width = letter
    c = canvas.Canvas(output, pagesize=letter)
    # try:
    for root, dirs, files in os.walk(dirim):
        for name in files:
            lname = name.lower()
            if lname.endswith(".JPG") or lname.endswith(".gif") or      lname.endswith(".png"):
                filepath = os.path.join('/home/deepak/Downloads', 'image')
                c.drawImage('/home/deepak/Downloads/imag.pdf', inch, inch * 1)
                c.showPage()
                c.save()
                return output
    #   print "PDF of Image directory created"
    # except:
    #  print "Failed creating PDF"
4

1 回答 1

1

如果代码从未在 for 循环中输入 if 语句,则函数将返回None不是HttpResponse对象。在您的 if 语句中,您将返回一个字符串,该字符串也不是HttpResponse对象。只需将其包装在一个HttpResponse对象中:

for root, dirs, files in os.walk(dirim):
    for name in files:
        lname = name.lower()
        if lname.endswith(".JPG") or lname.endswith(".gif") or lname.endswith(".png"):
            filepath = os.path.join('/home/deepak/Downloads', 'image')
            c.drawImage('/home/deepak/Downloads/imag.pdf', inch, inch * 1)
            c.showPage()
            c.save()                    
            return HttpResponse(output)
return HttpResponse("Failed creating PDF")
于 2013-01-31T08:53:59.300 回答