2015-05-31 84 views
2

我在我的燒瓶應用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) 

的,但是當我試圖做出端點/回購/請求,我得到這個錯誤。

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' 

回答

2

的全面回溯只是把這些過濾器定義的模塊並不運行它們,你需要在創建應用程序後,實際導入模塊。這將運行代碼並註冊過濾器。

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

這將是一個通告。我在過濾器中導入APP,如果過濾器將被導入__init__.py文件中,我正在實例化瓶子對象,那麼它不會是一個循環調用。順便說一句,我試過,我得到錯誤。欲瞭解更多信息,您可以在[github](https://github.com/rahulrrixe/fresque/tree/git-integeration)上查看該項目。 – Rahul

+0

我收到此錯誤。 回溯(最近通話最後一個): 文件 「runserver.py」,10號線,在 從fresque進口APP 文件 「/Users/rranjan/Python/fresque/fresque/__init__.py」,13號線,在 從fresque進口過濾器 文件「/Users/rranjan/Python/fresque/fresque/filters.py」,8號線,在 從fresque進口APP 導入錯誤:無法導入名稱APP – Rahul

+0

你是對的。在實例化應用程序對象之前,我錯誤地導入了過濾器。 – Rahul