如何从视图中禁用Django的自动映像? [英] How do I disable Django's autoescape from a view?

查看:149
本文介绍了如何从视图中禁用Django的自动映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django说有3种方法可以关闭autoescape:


  1. 使用 | safe 变量之后

  2. 使用 {%autoescape on%} {%endautoescape%}

  3. 使用上下文,如 context = Context({'message':message},autoescape = False)

(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''& amp ;&安培; LT;&安培; GT; 

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):
返回一个字符串

加载给定的template_name,并给出给定的块,
给定字典作为上下文


dictionary = dictionary或{}
t = _get_template(template_name)
如果context_instance:
context_instance.update(dictionary)
else:
context_instance =上下文(字典)
返回render_template_block(t,块,context_instance)

第三个arg应该是一个dict,而不是context。否则会使用正常的上下文实例。



所以我相信应该是:

 code> render_block_to_string(template_name,'ios_message',{},context)

希望它有帮助。


Django says there's 3 ways to turn off autoescape:

  1. Use |safe after the variable
  2. Use {% autoescape on %} and {% endautoescape %} within blocks
  3. Use a Context like context = Context({'message': message}, autoescape=False)

(1) and (2) work fine. But I have the situation where I have templates to generate plain-text push notifications, and I have LOADS of templates to build and maintain. I could go through and put the {% autoescape on %} and {% endautoescape %} tags in all of them, but (3) should allow me to do it in one line in the view.

The template:

{% block ios_message %}{{message}}{% endblock %}

The view:

message = u"'&<>"
context = Context({'message': message}, autoescape=False)
render_block_to_string(template_name, 'ios_message', context)

The output:

u'&#39;&amp;&lt;&gt;

The code for block_render.py is from here: https://github.com/uniphil/Django-Block-Render/blob/master/block_render.py. I'm using it as is from there.

Anyone know what gives?

解决方案

Take a closer look to function 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, block, context_instance)

The 3rd arg should be a dict, not context. Otherwise it would use the normal context instance.

So I believe it should be:

render_block_to_string(template_name, 'ios_message', {},  context)

Hope it helps.

这篇关于如何从视图中禁用Django的自动映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆