0

render_to_response を使用して返された html をエスケープする必要があります。適切なドキュメントが見つかりません。誰かが何かの方向を指すことができますか?

コードは次のとおりです。

return render_to_response(template_name, {return render_to_response(template_name, {
            'form': form,
            redirect_field_name: redirect_to,
            'site': current_site,
            'site_name': current_site.name,
        }, context_instance=RequestContext(request))

ここでは、応答の HTML テキストをエスケープする必要があります。私が知っている方法は、テンプレートファイルを文字列で読み取り、 re.escape() でエスケープしてからレンダリングすることです。それを行うためのよりクリーンで簡単な方法は何ですか??

4

3 に答える 3

2

応答全体をエスケープしたいようですが、本当ですか?少し奇妙に思えますが、確かにそれはできます。

import django.utils.html as html
string = render_to_string(<all of the args to render_to_response>)
escaped_string = html.escape(string)
return HttpResponse(escaped_string)

相手の人/ブラウザがこれでをするのかよくわかりませんが、あなたが求めていたように聞こえます。

于 2009-12-22T18:33:51.563 に答える
2

ビューからテンプレートに渡される文字列はrender_to_response、デフォルトでエスケープされます。

参照: http://docs.djangoproject.com/en/dev/topics/templates/#id2

Django のデフォルトでは、すべてのテンプレートはすべての変数タグの出力を自動的にエスケープします。具体的には、次の 5 文字がエスケープされます。

< is converted to &lt;
> is converted to &gt;
' (single quote) is converted to &#39;
" (double quote) is converted to &quot;
& is converted to &amp;

繰り返しますが、この動作はデフォルトでオンになっていることを強調します。

于 2009-12-22T13:37:22.410 に答える
1

escapeテンプレート フィルターまたはautoescapeテンプレート タグを使用できます。http://docs.djangoproject.com/en/dev/ref/templates/builtins/を参照してください。

于 2009-12-22T13:41:28.533 に答える