0

I use Django 1.8, reportlab and xhtml2pdf, html5lib, pypdf and the view goes like this (https://stackoverflow.com/a/1377652/4496057)

def render_to_pdf(template_src, context_dict, request):
    template = get_template(template_src)
    context = Context(site_pages(request))
    html = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result)
    if not pdf.err:
        return HttpResponse(content=result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

and im getting the following error Update(Entire traceback):

Internal Server Error: /system/MNE/1212/doctor/summary-pdf/8/123123123
Traceback (most recent call last):
  File "/home/vagrant/sat/lib/python2.7/site-packages/django/core/handlers/base.py", line 164, in get_response
    response = response.render()
  File "/home/vagrant/sat/lib/python2.7/site-packages/django/template/response.py", line 158, in render
    self.content = self.rendered_content
  File "/home/vagrant/sat/lib/python2.7/site-packages/django/template/response.py", line 135, in rendered_content
    content = template.render(context, self._request)
  File "/home/vagrant/sat/lib/python2.7/site-packages/django/template/backends/django.py", line 72, in render
    context = make_context(context, request)
  File "/home/vagrant/sat/lib/python2.7/site-packages/django/template/context.py", line 272, in make_context
    context.push(original_context)
  File "/home/vagrant/sat/lib/python2.7/site-packages/django/template/context.py", line 55, in push
    return ContextDict(self, *args, **kwargs)
  File "/home/vagrant/sat/lib/python2.7/site-packages/django/template/context.py", line 20, in __init__
    super(ContextDict, self).__init__(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 5918; 2 is required

This error seems specific to HTTPResponse and the other stackoverflow posts aren't helpful

Community
  • 1
  • 1
fiskrisktisk
  • 158
  • 1
  • 12
  • Does the traceback say which line of _your_ code is causing the error? – John Gordon May 11 '16 at 19:44
  • Also, what does `render_to_pdf()` return if `pdf.err` is truthy? In the post you linked, there's a catchall `return HTTPResponse(...)` as the last line of `render_to_pdf()`, which your code does not have. – John Gordon May 11 '16 at 19:48
  • 1
    we need the entire traceback, or at least the part that references your code above! otherwise we're just guessing (e.g. `site_pages` doesn't return something that can be passed directly to `Context()` would be a guess..) – thebjorn May 11 '16 at 20:07
  • pdf.err is false in this case and the execution hits the HTTPResponse() line – fiskrisktisk May 11 '16 at 20:13
  • Can you write `result.getvalue()` to a file (and does it open as valid pdf?) – thebjorn May 11 '16 at 20:18
  • Yes I'm able to do that open the pdf fine – fiskrisktisk May 11 '16 at 20:26
  • Ok I have figured out what the issue is here. On my View I was inheriting TemplateView and removing that fixed it – fiskrisktisk May 11 '16 at 20:41

0 Answers0