On 9 Jun 2006, at 14:03, Spock wrote:
> I've application where most of data is fetched from database.
> Those data are inserted by people without "trust", so in every
> template
> I'm using |escape filter ...so a question is :
> Is there is some method to enable global escape filter ? :)
I've been thinking about this recently, and I've come to the
conclusion that we might have missed a trick by not making ALL
replacement variables escaped by default (and including a var|raw
filter for the times when you don't want stuff to be escaped). It's
probably too late to change this now though.
One solution is to write your own custom Context class and use that.
The following code is unteste:
from django.template.context import Context
from django.utils.html import escape
class EscapedContext(Context):
def __getitem__(self, key):
value = super(Context, self)[key]
return escape(value)
You would also need to add your own 'unescape' custom template filter
that reverses the effects of escape for cases where you needed to do
that. Maybe unescape would be a useful addition to the default set of
template tags...
Cheers,
Simon