flask 应用程序中 jinja2 template_filter 装饰器的异常行为

Unusual behavior of jinja2 template_filter decorator in flask application

我的 Flask 应用程序中有一个 filters.py 文件,其内容是:

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals, print_function
import arrow
from fresque import APP


@APP.template_filter('short')
def shorted_commit(cid):
    """Gets short version of the commit id"""
    return cid[:6]


@APP.template_filter('humanize')
def humanize_date(date):
    """ Template filter returning the last commit date of the provided repo.
    """
    return arrow.get(date).humanize()

现在我尝试在 html 文件中使用这个模板过滤器,内容如下:

{% extends 'layout.html' %}
{% import 'macros.html' as macros %}
{% block content %}
{% if tree %}
        {% for entry in tree %}
                <span> {{ entry.name }} </span>
                <span> {{ entry.hex|short}} </span>
        {% endfor %}
  {% endif %}
{% endblock %}

此外,呈现此页面的控制器函数也被写入并保存在名为 gitview.py 的文件中。此文件位于与 filters.py

相同的文件夹中
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals, print_function

import os
import flask
import pygit2

from fresque import APP
from fresque.lib.git import Repository


@APP.route("/repo/<name>")
def repo_base_view(name):
    path = os.path.join(
        APP.config['GIT_DIRECTORY_PATH'], flask.g.fas_user.username)

    try:
        repo_obj = Repository(os.path.join(path, name))
    except IOError:
        return "No such repo", 404

    cnt = 0
    last_commits = []
    tree = []
    if not repo_obj.is_empty:
        try:
            for commit in repo_obj.walk(
                    repo_obj.head.target, pygit2.GIT_SORT_TIME):
                last_commits.append(commit)
                cnt += 1
                if cnt == 3:
                    break
            tree = sorted(last_commits[0].tree, key=lambda x: x.filemode)
        except pygit2.GitError:
            pass
    return flask.render_template('/git/repo.html', tree=tree)

但是当我尝试向端点 /repo/ 发出请求时,出现此错误。

TemplateAssertionError: no filter named 'short'

这是错误的完整回溯

127.0.0.1 - - [01/Jun/2015 02:37:28] "GET /repo/spiderman HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/rranjan/Python/fresque/fresque/proxy.py", line 50, in __call__
    return self.app(environ, start_response)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/rranjan/Python/fresque/fresque/gitview.py", line 41, in repo_base_view
    return flask.render_template('/git/repo.html', tree=tree)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/flask/templating.py", line 127, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/jinja2/environment.py", line 791, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/jinja2/environment.py", line 765, in _load_template
    template = self.loader.load(self, name, globals)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/jinja2/loaders.py", line 125, in load
    code = environment.compile(source, name, filename)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/jinja2/environment.py", line 554, in compile
    self.handle_exception(exc_info, source_hint=source)
  File "/Users/rranjan/.virtualenvs/fresque/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/rranjan/Python/fresque/fresque/templates/git/repo.html", line 15, in <module>
    <span class="filehex" >{{ entry.hex|short }}</span>
TemplateAssertionError: no filter named 'short'

只是将这些过滤器定义放在模块中并不能 运行 它们,您需要在创建应用程序后实际导入模块。这将 运行 代码并注册过滤器。

# ...
app = Flask(__name__)
# ...
from fresque import filters