Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in ability to use render_to_string() #21

Closed
peterbe opened this issue Feb 29, 2012 · 1 comment
Closed

Bug in ability to use render_to_string() #21

peterbe opened this issue Feb 29, 2012 · 1 comment

Comments

@peterbe
Copy link

peterbe commented Feb 29, 2012

This works in regular django:

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:

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
        ...
@peterbe
Copy link
Author

peterbe commented Feb 29, 2012

Crap! I misinterpreted the documentation. If you're not going to send a simple dict you use the context_instance like this:

ccontext = Context(context)
html = render_to_string('homepage/test.html', context_instance=ccontext)

The original reason for investigating this is probably where the fault is at.

@peterbe peterbe closed this as completed Feb 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant