开发者

Is it possible to pass SafeData instances to Django templates?

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?

f开发者_如何学Gorom 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 }}.


Yes, this will work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜