如何从视图中禁用Django的autoescape?
发布时间:2020-12-20 13:36:35 所属栏目:Python 来源:网络整理
导读:Django说有3种方法可以关闭autoescape: 变量后使用| safe 在块中使用{%autoescape on%}和{%endautoescape%} 使用context = Context({‘message’:message},autoescape = False) (1)和(2)工作正常.但我有这样的情况,我有模板生成纯文本推送通知,我有模
Django说有3种方法可以关闭autoescape:
>变量后使用| safe (1)和(2)工作正常.但我有这样的情况,我有模板生成纯文本推送通知,我有模板的LOADS来构建和维护.我可以通过并在所有这些中放置{%autoescape on%}和{%endautoescape%}标签,但是(3)应该允许我在视图中的一行中执行此操作. 模板: {% block ios_message %}{{message}}{% endblock %} 风景: message = u"'&<>" context = Context({'message': message},autoescape=False) render_block_to_string(template_name,'ios_message',context) 输出: u''&<> block_render.py的代码来自这里:https://github.com/uniphil/Django-Block-Render/blob/master/block_render.py.我正在那里使用它. 谁知道什么给了? 解决方法
仔细看看render_block_to_string()函数:
def render_block_to_string(template_name,block,dictionary=None,context_instance=None): """Return a string Loads the given template_name and renders the given block with the given dictionary as context. """ dictionary = dictionary or {} t = _get_template(template_name) if context_instance: context_instance.update(dictionary) else: context_instance = Context(dictionary) return render_template_block(t,context_instance) 第三个arg应该是一个dict,而不是上下文.否则它将使用普通的上下文实例. 所以我认为它应该是: render_block_to_string(template_name,{},context) 希望能帮助到你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |