Flask Security - TemplateAssertionError:没有名为'urlencode'的过滤器

时间:2015-01-22 15:22:50

标签: python-2.7 flask openshift flask-security

我刚刚将烧瓶安全性添加到我的烧瓶项目中。它在本地工作,但在OpenShift上报告此错误:

TemplateAssertionError: no filter named 'urlencode'

我不知道它是否是一个错误的库版本,或者如何调试它。这是我的setup.py包列表:

install_requires=['Flask==0.10.1',
                    'SQLAlchemy==0.9.8',
                    'Flask-SQLAlchemy==2.0',
                    'Flask-Security==1.7.4',
                    'Werkzeug==0.9.5',
                    'blinker==1.3',
                    'Flask-Login==0.2.11',
                    'Flask-Mail==0.9.1',
                    'Flask-Principal==0.4.0',
                    'Flask-Script==2.0.5',
                    'Flask-WTF==0.10.3',
                    'itsdangerous==0.24',
                    'passlib==1.6.2'

  ]

2 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,通过冻结' pip冻结'在我的本地计算机上,并逐个将库复制到setup.py.虽然我还不确定哪一个产生了错误,但可能是错误版本的jinja2。

答案 1 :(得分:0)

The urlencode filter was added to jinja in v2.7. But GAE only supports v2.6. Changing the version to "latest" in my app.yaml still runs with 2.6 (notice the python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py path):

...
 File "/base/data/home/apps/s~healthier-staging/1.386037228785871893/lib/flask/templating.py", line 128, in render_template
context, ctx.app)
  File "/base/data/home/apps/s~healthier-staging/1.386037228785871893/lib/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
  File "/base/data/home/apps/s~healthier-staging/1.386037228785871893/lib/flask_security/templates/security/_menu.html", line 4, in template
<li><a href="{{ url_for_security('login') }}{% if 'next' in request.args %}?next={{ request.args.next|urlencode }}{% endif %}">Login</a></li>
TemplateAssertionError: no filter named 'urlencode'`

I fixed this by adding a simple filter (copying the code that was added to jinja) manually:

def do_urlescape(value):
    """Escape for use in URLs."""
    return urllib.quote(value.encode('utf8'))
app.jinja_env.globals['urlencode'] = do_urlescape