Python 如何使用Django输出PDF?

Python 如何使用Django输出PDF?,python,django,pdf,django-views,pdf-generation,Python,Django,Pdf,Django Views,Pdf Generation,我指的是使用reportlab生成pdf def some_view(request): # Create the HttpResponse object with the appropriate PDF headers. response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="somefilename.

我指的是使用reportlab生成pdf

def some_view(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'

    # Create the PDF object, using the response object as its "file."
    p = canvas.Canvas(response)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    logging.debug('-----p---')
    logging.debug(p)
    p.showPage()
    p.save()
    return response
如果我在myapp中添加此reportlab文件夹,则会出现以下错误:

ViewDoesNotExist at /aps/print-pdf/
Could not import myapp.views. Error was: No module named reportlab
ViewDoesNotExist at /aps/print-pdf/
Tried some_view in module myapp.views. Error was: 'HardenedModulesHook' object has no attribute '_files'
如果我将此reportlab文件夹从myapp文件夹中取出,则会出现以下错误:

ViewDoesNotExist at /aps/print-pdf/
Could not import myapp.views. Error was: No module named reportlab
ViewDoesNotExist at /aps/print-pdf/
Tried some_view in module myapp.views. Error was: 'HardenedModulesHook' object has no attribute '_files'
这有什么不对?请帮忙

  • 检查
    myapp
    是否在您的设置中。是否安装了应用程序
  • 检查
    reportlab
    是否成功安装在python环境中,使用
    pip freeze
    或运行python shell以查看
    import reportlab
    是否可以工作

  • 您是如何安装reportlab的?你使用virtualenv吗?