我決定安裝jinja2以與我的web應用程序一起使用,以支持autoescape功能。所以我將jinja2安裝到python 2.5中,並在我的項目中創建了一個指向該目錄的符號鏈接。它大多工作正常。谷歌應用引擎webapp中的jinja2 autoescape問題
EXCEPT,當我真正嘗試使用{%autoescape真正%}標籤,我得到的消息:
File "/Users/me/project/templates/_base.html", line 1, in template
{% autoescape true %}
TemplateSyntaxError: Encountered unknown tag 'autoescape'.
我使用的標籤,因爲他們有它在docs:
{% autoescape true %} stuff {{var1}} stuff {{var2}}{% endautoescape %}
在我的處理程序文件,我導入相關的東西:
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape
,並在導入工作正常,因爲它是不會拋出錯誤。我做錯了什麼,或者jinja2本身有問題,就像ext.py?
更新: 我試過下面沙爾斯的建議,並得到了相同的結果。這是我使用他的建議更新處理程序。
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標記,它就可以正常工作。
我剛剛注意到,jinja2自動完成標籤不能像tipfy框架中記錄的那樣工作。這導致我認爲這是jinja2中的一個bug,而不是我如何使用它的問題。 – 2011-01-12 23:10:27