【问题标题】:How to Outputting PDFs with Django?如何使用 Django 输出 PDF?
【发布时间】:2014-01-30 09:29:47
【问题描述】:

我指的是https://docs.djangoproject.com/en/dev/howto/outputting-pdf/ 用于使用 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

如果我将此 reportlab 文件夹从 myapp 文件夹中取出,则会出现此错误:

ViewDoesNotExist at /aps/print-pdf/
Tried some_view in module myapp.views. Error was: 'HardenedModulesHook' object has no attribute '_files'

这有什么问题?请帮忙。

【问题讨论】:

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

标签: python django pdf django-views pdf-generation


【解决方案1】:
  1. 检查myapp 是否在您的设置中。INSTALLED_APPS?
  2. 检查reportlab是否成功安装在你的python环境中,使用pip freeze或者运行python shell看看import reportlab是否可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    相关资源
    最近更新 更多