2017-06-21 10 views
0

我想我的HTML转换成PDF格式,但我收到以下错误将HTML转换为PDF格式:使用Django

'dict' object has no attribute 'render_context'.

的代码如下:

utils.py

from io import BytesIO 
from django.http import HttpResponse 
from django.template.loader import get_template 

from xhtml2pdf import pisa 


def render_to_pdf(template_src, context_dict={}): 
    template = get_template(template_src) 
    html = template.render(context_dict) 
    result = BytesIO() 
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result) 
    if not pdf.err: 
     return HttpResponse(result.getvalue(), content_type='application/pdf') 
    return None 

views.py

from django.http import HttpResponse 
import datetime 
from django.template.loader import get_template 
from admin.views.utils import render_to_pdf # created in step 4 


def get_pdf_view(request, *args, **kwargs): 
    template = get_template('certificate_appreciation_pdf.html') 
    context = { 
     'today': datetime.date.today(), 
     'amount': 39.99, 
     'customer_name': 'Cooper Mann', 
     'order_id': 1233434, 
    } 
    html = template.render(context) 
    pdf = render_to_pdf('certificate_appreciation_pdf.html', context) 

certificate_appreciation_pdf.html

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <title>Title</title> 
    </head> 
    <body> 
     <div class='wrapper'> 
      <div class='header'> 
       <p class='title'>Invoice # </p> 
      </div> 
     <div> 
     <div class='details'> 
      Bill to: {{ order_id }}<br/> 
      Amount: {{ amount }} <br/> 
      Date: {{ today }} 
      <hr class='hrItem' /> 
     </div> 
    </div> 
    </body> 
</html> 

我不知道我在哪里发生错误。 错误我渲染到该页面

AttributeError at /pdf/ 
'dict' object has no attribute 'render_context' 
Request Method: GET 
Request URL: http://0.0.0.0:8001/pdf/ 
Django Version: 1.5.5 
Exception Type: AttributeError 
Exception Value:  
'dict' object has no attribute 'render_context' 
Exception Location: /usr/local/lib/python2.7/dist-packages/django/template/base.py in render, line 138 
Python Executable: /usr/bin/python 
Python Version: 2.7.6 
Python Path:  
['/home/linux/Desktop/project_directory/nasscom-final/django-pursuite/pursuite/../apps', 
'./apps', 
'/home/linux/Desktop/project_directory/nasscom-final/django-pursuite', 
'/usr/local/lib/python2.7/dist-packages/Pursuite-1.7.18-py2.7.egg', 
'/usr/local/lib/python2.7/dist-packages/boto-2.45.0-py2.7.egg', 
'/usr/local/lib/python2.7/dist-packages/django_pagination-1.0.7-py2.7.egg', 
'/usr/local/lib/python2.7/dist-packages/Jinja2-2.9.3-py2.7.egg', 
'/usr/local/lib/python2.7/dist-packages/Sphinx-1.5.1-py2.7.egg', 
'/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PILcompat', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/pymodules/python2.7', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client'] 
Server time: Wed, 21 Jun 2017 14:34:08 +0530 
+0

请问您可以粘贴所有日志吗? –

+0

[Django on Apache web server'dict'object has no attributes'render \ _context']可能重复(https://stackoverflow.com/questions/6333367/django-on-apache-web-server-dict-object- has-no-attribute-render-context) –

+0

http://weasyprint.readthedocs.io/是将HTML转换为PDF的最佳选择。 –

回答

1

您使用的是非常旧的(和不支持)版本的Django的时候了。在该版本中,您需要将Context实例传递到template.render,而不是字典。

from django.template import Context 
... 
context = Context({...}) 
html = template.render(context) 

但是,你必须升级。目前没有理由使用Django 1.5。

+0

感谢它现在工作f9 ....如果我升级到django 1.10或1.9我需要更改安装的软件包或代码,因为它是一个在django 1.5上运行的大项目。 –