rest-framework-latex icon indicating copy to clipboard operation
rest-framework-latex copied to clipboard

Django 1.10: ValueError: dictionary update sequence element #0 has length xx; 2 is required

Open florianm opened this issue 9 years ago • 0 comments

Hi folks, just got first light on some sweet PDF output from your neat extension but I'm running into a weird bug in django-rest-framework.

Environment

  • Django 1.10.2
  • django-rest-swagger==2.0.7
  • djangorestframework==3.5.0
  • djangorestframework-csv==1.4.1
  • djangorestframework-filters==0.8.1
  • djangorestframework-gis==0.10.1
  • djangorestframework-jsonp==1.0.2
  • djangorestframework-yaml==1.0.3
  • dynamic-rest==1.5.0
  • rest-framework-latex==0.0.9

config:

TEMPLATES = [
    {
        # See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
[...]

Expected

PDF output via rest-framework-latex

Error

Accessing an API view with rest-framework-latex's renderer enabled throws a

ValueError
dictionary update sequence element #0 has length 13; 2 is required

Hack

Change rest_framework/compat.py from

# backends template, e.g. django.template.backends.django.Template
    else:
        return template.render(context, request=request)

to

# backends template, e.g. django.template.backends.django.Template
    else:
        return template.render(dict(data=context), request=request)

so I can access it in my latex template like so

{% for obj in data %}
 {{ obj.some_field_that_my_api_exports|latex_safe }}
{% endfor %}

context is originally an OrderedDict of my queryset. This is apparently absolutely required by all other renderers like the Browseable API renderer. However, I couldn't figure out a way to access the OrderedDict in my template without wrapping it into a dict.

Unless I'm missing a straight-forward way to access the OrderedDict from the template, would it be possible to change LatexRenderer.render() to wrap data into a normal dict?

        tex = super(LatexRenderer, self).render(
            {"data": data}, accepted_media_type, renderer_context)

Sending PR for review and discussion.

florianm avatar Oct 21 '16 03:10 florianm