4

Django によると、自動エスケープをオフにする方法は 3 つあります。

  1. |safe変数の後に使用
  2. ブロック内で{% autoescape on %}andを使用する{% endautoescape %}
  3. 次のようなコンテキストを使用しますcontext = Context({'message': message}, autoescape=False)

(1) と (2) は正常に動作します。しかし、プレーンテキストのプッシュ通知を生成するためのテンプレートがあり、作成して維持するテンプレートがたくさんある状況があります。{% autoescape on %}すべてのタグにandタグを追加することもでき{% endautoescape %}ますが、(3) ビュー内の 1 行で実行できるようにする必要があります。

テンプレート:

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

景色:

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

出力:

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

block_render.py のコードは、https ://github.com/uniphil/Django-Block-Render/blob/master/block_render.py からのものです。そこからそのまま使っています。

誰が何を与えるか知っていますか?

4

3 に答える 3

1

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)

3 番目の引数は、コンテキストではなく辞書である必要があります。それ以外の場合は、通常のコンテキスト インスタンスが使用されます。

だから私はそれがそうであるべきだと信じています:

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

それが役に立てば幸い。

于 2013-08-13T18:04:02.923 に答える