Ticket #18105: 18105-2.diff

File 18105-2.diff, 3.9 KB (added by Claude Paroz, 13 years ago)

Properly construct context when inclusion tag returns a context

  • django/contrib/admin/templatetags/admin_modify.py

    diff --git a/django/contrib/admin/templatetags/admin_modify.py b/django/contrib/admin/templatetags/admin_modify.py
    index e55b3bf..a269ee0 100644
    a b def prepopulated_fields_js(context):  
    1616            for inline_admin_form in inline_admin_formset:
    1717                if inline_admin_form.original is None:
    1818                    prepopulated_fields.extend(inline_admin_form.prepopulated_fields)
    19     context.update({'prepopulated_fields': prepopulated_fields})
    20     return context
     19    return {'prepopulated_fields': prepopulated_fields}
    2120
    2221@register.inclusion_tag('admin/submit_line.html', takes_context=True)
    2322def submit_row(context):
  • django/template/base.py

    diff --git a/django/template/base.py b/django/template/base.py
    index e2fc66b..c61f256 100644
    a b from functools import partial  
    55from inspect import getargspec
    66
    77from django.conf import settings
    8 from django.template.context import (Context, RequestContext,
     8from django.template.context import (BaseContext, Context, RequestContext,
    99    ContextPopException)
    1010from django.utils.importlib import import_module
    1111from django.utils.itercompat import is_iterable
    class Library(object):  
    11861186                        else:
    11871187                            t = get_template(file_name)
    11881188                        self.nodelist = t.nodelist
    1189                     new_context = context_class(_dict, **{
     1189                    context_vars = {
    11901190                        'autoescape': context.autoescape,
    11911191                        'current_app': context.current_app,
    11921192                        'use_l10n': context.use_l10n,
    11931193                        'use_tz': context.use_tz,
    1194                     })
     1194                    }
     1195                    if isinstance(_dict, BaseContext):
     1196                        new_context = _dict
     1197                        new_context.update(context_vars)
     1198                    else:
     1199                        new_context = context_class(_dict, **context_vars)
    11951200                    # Copy across the CSRF token, if present, because
    11961201                    # inclusion tags are often used for forms, and we need
    11971202                    # instructions for using CSRF protection to be as simple
  • tests/regressiontests/templates/custom.py

    diff --git a/tests/regressiontests/templates/custom.py b/tests/regressiontests/templates/custom.py
    index 6bbbe35..ad62be2 100644
    a b class CustomTagTests(TestCase):  
    181181            "'inclusion_unlimited_args_kwargs' received multiple values for keyword argument 'eggs'",
    182182            template.Template, '{% load custom %}{% inclusion_unlimited_args_kwargs 37 eggs="scrambled" eggs="scrambled" %}')
    183183
     184        t = template.Template('{% load custom %}{% inclusion_tag_returns_context %}')
     185        self.assertEqual(t.render(c), u'inclusion_returning_context\n')
     186
    184187    def test_include_tag_missing_context(self):
    185188        # The 'context' parameter must be present when takes_context is True
    186189        self.assertRaisesRegexp(template.TemplateSyntaxError,
  • tests/regressiontests/templates/templatetags/custom.py

    diff --git a/tests/regressiontests/templates/templatetags/custom.py b/tests/regressiontests/templates/templatetags/custom.py
    index 7f78831..cfa727f 100644
    a b def inclusion_tag_without_context_parameter(arg):  
    233233    return {}
    234234inclusion_tag_without_context_parameter.anything = "Expected inclusion_tag_without_context_parameter __dict__"
    235235
     236@register.inclusion_tag('inclusion.html', takes_context=True)
     237def inclusion_tag_returns_context(context):
     238    context.update({"result": "inclusion_returning_context"})
     239    return context
     240
    236241@register.assignment_tag
    237242def assignment_no_params():
    238243    """Expected assignment_no_params __doc__"""
Back to Top