jbalogh/jingo — Issue #21: Bug in ability to use render_to_string()
Author: peterbe
Created: 2012-02-29 02:41:01+00:00
State: open

This works in regular django:

```python
from django.test import TestCase
from django.template.loader import render_to_string
from django.template import Context

class SimpleTest(TestCase):

def test_render_to_string(self):

context = {'name': 'Peter'}
html = render_to_string('homepage/test.html', context)
assert 'Name:Peter' in html

ccontext = Context(context)
html = render_to_string('homepage/test.html', ccontext)
assert 'Name:Peter' in html
```

But if fails when using jingo. It fails when the context parameter is a real subclass of the `Context` class.
The documentation says you're supposed to be able to pass either a dict or a Context instance.
https://docs.djangoproject.com/en/dev/ref/templates/api/#the-render-to-string-shortcut

The traceback is this:

```
Traceback (most recent call last):
File "/Users/peterbe/dev/DJANGO/django-peterbecom/apps/homepage/tests.py", line 15, in test_render_to_string
html = render_to_string('homepage/test.html', ccontext)
File "/Users/peterbe/virtualenvs/peterbecom2/lib/python2.7/site-packages/django/template/loader.py", line 171, in render_to_string
return t.render(Context(dictionary))
File "/Users/peterbe/virtualenvs/peterbecom2/lib/python2.7/site-packages/jingo/__init__.py", line 172, in render
context_dict.update(d)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
```

The relevant code is this:

```python
class Template(jinja2.Template):

def render(self, context={}):
"""Render's a template, context can be a Django Context or a
dictionary.
"""
# flatten the Django Context into a single dictionary.
context_dict = {}
if hasattr(context, 'dicts'):
for d in context.dicts:
context_dict.update(d)
else:
context_dict = context
...

```