我正在尝试将HTML转换为PDF,但我收到以下错误:
'字典' object没有属性' render_context'。
代码如下:
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
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)
<!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 :(得分:1)
您使用的是非常旧(且不受支持)的Django版本。在该版本中,您需要将Context实例传递给template.render
,而不是dict。
from django.template import Context
...
context = Context({...})
html = template.render(context)
但是,必须升级。此时没有理由使用Django 1.5。