python 2.7 - Unusual behavior of jinja2 template_filter decorator in flask application -
i have filters.py file in flask application , content is:
# -*- coding: utf-8 -*- __future__ import absolute_import, unicode_literals, print_function import arrow fresque import app @app.template_filter('short') def shorted_commit(cid): """gets short version of commit id""" return cid[:6] @app.template_filter('humanize') def humanize_date(date): """ template filter returning last commit date of provided repo. """ return arrow.get(date).humanize()
now trying use template filter in html file content follows:
{% extends 'layout.html' %} {% import 'macros.html' macros %} {% block content %} {% if tree %} {% entry in tree %} <span> {{ entry.name }} </span> <span> {{ entry.hex|short}} </span> {% endfor %} {% endif %} {% endblock %}
also controller function renders pages written , saved in file named gitview.py. file resides in same folder of filters.py
# -*- coding: utf-8 -*- __future__ import absolute_import, unicode_literals, print_function import os import flask import pygit2 fresque import app 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: 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)
but when trying make request endpoint /repo/, getting error.
templateassertionerror: no filter named 'short'
here full traceback of error
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'
just putting filter definitions in module doesn't run them, need import module after creating app. run code , register filters.
# ... app = flask(__name__) # ... fresque import filters
Comments
Post a Comment