Using django_xhtml2pdf with django 1.11" error: context must be a dict rather than Context."
Using django_xhtml2pdf with django 1.11" error: context must be a dict rather than Context."
根据其中一条评论:我确实将代码更改为:
providers = Provider.objects.all()
context = { 'providers':providers}
我知道这没有什么不同,但我想无论如何我都会尝试一下,因为发生了一些奇怪的事情。我担心错误出在模块本身 运行 上,在我的 django 版本中。
我确实看到了其他答案,这让我感到困惑,因为我只是使用此处记录的内容:
https://spapas.github.io/2015/11/27/pdf-in-django/#django-integration
让 django_xhtml2pdf 工作正常。我的看法是这样的:
def providers_plain_old_view(request):
resp = HttpResponse(content_type='application/pdf')
context = {
'providers': Provider.objects.all()
}
result = generate_pdf('ipaswdb/provider/providers_plain_old_view.html', file_object=resp, context=context)
return result
我现在知道在我正在使用的 django 1.11.14 中有问题,但不知道如何修复错误:
Traceback (most recent call last):
File "D:\Python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Programming\web\ipa_django\mysite\ipaswdb\views.py", line 312, in providers_plain_old_view
result = generate_pdf('ipaswdb/provider/providers_plain_old_view.html', file_object=resp, context=context)
File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 62, in generate_pdf
generate_pdf_template_object(tmpl, file_object, context)
File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 39, in generate_pdf_template_object
html = template_object.render(Context(context))
File "D:\Python27\lib\site-packages\django\template\backends\django.py", line 64, in render
context = make_context(context, request, autoescape=self.backend.engine.autoescape)
File "D:\Python27\lib\site-packages\django\template\context.py", line 287, in make_context
raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than Context.
"GET /ipaswdb/provider_roster/ HTTP/1.1" 500 86485
我的意思是它要我在最新的 django 版本中以不同的方式调用 generate_pdf 函数?
主要问题出在
行
File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 39, in generate_pdf_template_object
html = template_object.render(Context(context))
在错误输出中。这是 django-xhtml2pdf 包不是最新的 1.11 的问题。对渲染的调用已从
html = template_object.render(Context(context))
到
html = template_object.render(context)
根据升级到1.11的注意事项https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/
django.template.loader节。
您可以等待他们通过提交错误报告来修复它,或者在您的 views.py
中实施软件包提供的功能
根据其中一条评论:我确实将代码更改为:
providers = Provider.objects.all()
context = { 'providers':providers}
我知道这没有什么不同,但我想无论如何我都会尝试一下,因为发生了一些奇怪的事情。我担心错误出在模块本身 运行 上,在我的 django 版本中。
我确实看到了其他答案,这让我感到困惑,因为我只是使用此处记录的内容:
https://spapas.github.io/2015/11/27/pdf-in-django/#django-integration
让 django_xhtml2pdf 工作正常。我的看法是这样的:
def providers_plain_old_view(request):
resp = HttpResponse(content_type='application/pdf')
context = {
'providers': Provider.objects.all()
}
result = generate_pdf('ipaswdb/provider/providers_plain_old_view.html', file_object=resp, context=context)
return result
我现在知道在我正在使用的 django 1.11.14 中有问题,但不知道如何修复错误:
Traceback (most recent call last):
File "D:\Python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Programming\web\ipa_django\mysite\ipaswdb\views.py", line 312, in providers_plain_old_view
result = generate_pdf('ipaswdb/provider/providers_plain_old_view.html', file_object=resp, context=context)
File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 62, in generate_pdf
generate_pdf_template_object(tmpl, file_object, context)
File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 39, in generate_pdf_template_object
html = template_object.render(Context(context))
File "D:\Python27\lib\site-packages\django\template\backends\django.py", line 64, in render
context = make_context(context, request, autoescape=self.backend.engine.autoescape)
File "D:\Python27\lib\site-packages\django\template\context.py", line 287, in make_context
raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than Context.
"GET /ipaswdb/provider_roster/ HTTP/1.1" 500 86485
我的意思是它要我在最新的 django 版本中以不同的方式调用 generate_pdf 函数?
主要问题出在
行File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 39, in generate_pdf_template_object
html = template_object.render(Context(context))
在错误输出中。这是 django-xhtml2pdf 包不是最新的 1.11 的问题。对渲染的调用已从
html = template_object.render(Context(context))
到
html = template_object.render(context)
根据升级到1.11的注意事项https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/ django.template.loader节。
您可以等待他们通过提交错误报告来修复它,或者在您的 views.py
中实施软件包提供的功能