5

自動エスケープ機能をサポートするために、webapp アプリケーションで使用する jinja2 をインストールすることにしました。そこで、jinja2 を python 2.5 にインストールし、プロジェクト内にそのディレクトリを指すシンボリック リンクを作成しました。おおむね正常に動作しています。

例外として、実際に {% autoescape true %} タグを使用しようとすると、次のメッセージが表示されます。

File "/Users/me/project/templates/_base.html", line 1, in template
    {% autoescape true %}
TemplateSyntaxError: Encountered unknown tag 'autoescape'.

ドキュメントにあるタグを使用しています:

{% autoescape true %} stuff {{var1}} stuff {{var2}}{% endautoescape %}

私のハンドラファイル内で、関連するものをインポートしています:

from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape

エラーがスローされていないため、インポートは正常に機能しています。私は何か間違ったことをしていますか、それとも ext.py のように jinja2 自体に問題がありますか?


更新: 以下の sharth の提案を試してみましたが、同じ結果が得られました。彼の提案を使用して更新されたハンドラーを次に示します。

class MainHandler(BaseHandler):
    def get(self):

        self.context['testEscape']='<script type="javascript">alert("hi");</script>'
        env = Environment(loader=FileSystemLoader([os.path.join(os.path.dirname(__file__), 'templates')]), autoescape=False)
        template = env.get_template('index.html')
        content = template.render(self.context)
        self.response.out.write(content)

繰り返しますが、autoescape タグを使用しない限り、問題なく動作します。

4

1 に答える 1

8

タグには、{% autoescape %}Jinja 2.4 以降とjinja2.ext.autoescape拡張機能のロードが必要です。

env = Environment(autoescape=True, extensions=['jinja2.ext.autoescape'],
                  loader=...)
于 2011-01-13T07:47:31.857 に答える