Python 使用Django将HTML转换为PDF
我正在尝试将HTML转换为PDF,但出现以下错误: “dict”对象没有属性“render\u context” 代码如下: utils.py views.py 证书_赞赏_pdf.htmlPython 使用Django将HTML转换为PDF,python,django,Python,Django,我正在尝试将HTML转换为PDF,但出现以下错误: “dict”对象没有属性“render\u context” 代码如下: utils.py views.py 证书_赞赏_pdf.html 您使用的是非常旧(且不受支持)的Django版本。在该版本中,您需要将上下文实例传递给template.render,而不是dict from django.template import Context ... context = Context({...}) html = template.render
您使用的是非常旧(且不受支持)的Django版本。在该版本中,您需要将上下文实例传递给
template.render
,而不是dict
from django.template import Context
...
context = Context({...})
html = template.render(context)
但是,您必须升级。现在没有理由使用Django 1.5。请粘贴所有日志好吗?可能的副本是将HTML转换为PDF的最佳选择。谢谢,f9正在工作……如果我升级到Django 1.10或1.9,我需要更改安装的软件包或代码,因为这是一个在Django 1.5上运行的大型项目。
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
from django.template import Context
...
context = Context({...})
html = template.render(context)