Using django_xhtml2pdf with django 1.11" error: context must be a dict rather than Context."

541 views Asked by At

Per one of the comments: I did change my code to:

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

I know it didn't make a difference but figured I would try it anyway cause stranger things have happened. I am worried the error is within the module itself running on my version of django here.

I did see the other answers on this, and it confused me because I am just using what was documented here:

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

for getting the django_xhtml2pdf stuff to work. My view is as such:

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

Which I know now is bad in django 1.11.14 I am using, but no idea how to fix the error:

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

I mean it wants me to call the generate_pdf function a different way in the latest django version?

1

There are 1 answers

0
Antonio Arredondo On BEST ANSWER

The main issue lies in the line

File "D:\Python27\lib\site-packages\django_xhtml2pdf\utils.py", line 39, in generate_pdf_template_object
html = template_object.render(Context(context))

in the error output. This is an issue the django-xhtml2pdf package not being up to date for 1.11. The call to render has changed from

html = template_object.render(Context(context))

to

html = template_object.render(context)

according to the upgrading to 1.11 notes https://docs.djangoproject.com/en/1.11/ref/templates/upgrading/ django.template.loader section.

You can either wait for them to fix it, by submitting a bug report or implement the functionality the package provides in your views.py