在Django 1.11中使用Django_xhtml2pdf“错误:上下文必须是dict而不是context。”

2024-06-16 14:54:19 发布

您现在位置:Python中文网/ 问答频道 /正文

根据其中一条评论:我确实将代码改为:

providers = Provider.objects.all()
context = { 'providers':providers}

我知道这没什么不同,但我想无论如何我都会试试,因为发生了一些奇怪的事情。我担心错误是在我的django版本上运行的模块本身。在

我确实看到了其他答案,这让我很困惑,因为我使用的只是这里记录的:

https://spapas.github.io/2015/11/27/pdf-in-django/#django-integration

让django_xhtml2pdf的东西工作。我的观点是:

^{pr2}$

我现在知道在我使用的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函数?在


Tags: djangoinpygetpdfresponserequestlib
1条回答
网友
1楼 · 发布于 2024-06-16 14:54:19

主要问题在于线路

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版本中没有更新的问题。对呈现的调用已从更改

^{pr2}$

html = template_object.render(context)

根据升级到1.11的注释https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/ django.template.loader第节。在

您可以等待他们修复它,通过提交一个bug报告,或者实现包在您的视图.py在

相关问题 更多 >