Hi Chris,

SmileyChris wrote:
> So my template looks like: {{ group|caps }} (`group` is a Model object
> and the `caps` filter just capitalizes the first letter) and I'm stuck
> with double escaping.
> 
> The problem is, that it still gets double-escaped. Django's
> FilterExpression checks to see if the incoming object is SafeData, but
> at this stage it is a Model object - it hasn't be translated to its
> __unicode__ value yet.
> 
> I thought I had found the solution by applying the stringfilter
> decorator to my filter but that doesn't help either, because that
> still only gets applied after FilterExpression does its check.

VariableNode always uses force_unicode to solve the problem that you
describe. It seems that FilterExpression is guilty:

In django/template/__init__.py, class FilterExpression:

    def resolve(self, context, ignore_failures=False):
        try:
            obj = self.var.resolve(context)
        except VariableDoesNotExist:
            if ignore_failures:
                obj = None
            else:
                if settings.TEMPLATE_STRING_IF_INVALID:
                    global invalid_var_format_string
                    if invalid_var_format_string is None:
                        invalid_var_format_string = '%s' in
settings.TEMPLATE_STRING_IF_INVALID
                    if invalid_var_format_string:
                        return settings.TEMPLATE_STRING_IF_INVALID % self.var
                    return settings.TEMPLATE_STRING_IF_INVALID
                else:
                    obj = settings.TEMPLATE_STRING_IF_INVALID
        for func, args in self.filters:
            arg_vals = []
            for lookup, arg in args:
                if not lookup:
                    arg_vals.append(mark_safe(arg))
                else:
                    arg_vals.append(arg.resolve(context))
            if getattr(func, 'needs_autoescape', False):
                new_obj = func(obj, autoescape=context.autoescape, *arg_vals)
            else:
                new_obj = func(obj, *arg_vals)
            if getattr(func, 'is_safe', False) and isinstance(obj, SafeData):
                                                             ^^^^^

                obj = mark_safe(new_obj)
            elif isinstance(obj, EscapeData):
                            ^^^^
                obj = mark_for_escaping(new_obj)
            else:
                obj = new_obj
        return obj

In the two places marked with ^^^^, you need to test force_unicode(obj)
instead of obj (in the isinstance test). But this means calling
force_unicode() twice (once in VariableNode.render, once in
FilterExpression.render), not nice.

I cannot dig into this more since I'm in a hurry, hope this helps a bit
before Malcolm can come up with a proper solution.


Michael


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to