views:

6

answers:

1

I know that it's possible to have a template filter return a SafeData instance by doing something similar to the following.

from fictitious import guaranteed_safe

from django.utils.safestring import mark_safe

def myfilter(text):
    return mark_safe(guaranteed_safe(text))

My question is whether it's possible to "mark as safe" variables in a view before passing them to a template. Would this work as expected?

from fictitious import guaranteed_safe

from django.utils.safestring import mark_safe

def myview(request, text):
    return render_to_response('index.html', {
        'text': mark_safe(guaranteed_safe(text))
    })

The reason that I'm keen to mark variables as safe from within views is that other people are likely to create templates to work with the views and I want template authors to be able to write {{ document.html }} rather than {{ document.html|safe }}.

A: 

Yes, this will work fine.

Daniel Roseman
Thanks, Daniel. I really could have tested this, but it was a case of starting to write a question and then realizing the likely solution. :)
davidchambers