2014-01-11 28 views
0

我指的是https://docs.djangoproject.com/en/dev/howto/outputting-pdf/用於生成pdf的reportlab。如何用Django輸出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' 

這是什麼問題?請幫忙。

+0

你如何安裝ReportLab的?你使用virtualenv嗎? – sneawo

回答

0
  1. 檢查myapp是否在您的settings.INSTALLED_APPS?
  2. 檢查reportlab成功安裝在你的Python環境,使用pip freeze或運行Python外殼,看是否能import reportlab工作