lundi 27 juin 2016

Flask with Jinja2 throws AttributeError: 'TemplateData' object has no attribute 'data'

I am trying to render a simple Jinja template. No matter what the template is, or what variables are passed to it, I get an error when trying to render. Why am I getting this error and how do I fix it?

frontend = Blueprint('frontend', __name__, template_folder='templates')

@frontend.route('/')
def index():
    stuff = dict()
    stuff['page_title'] = 'Something'
    stuff['center_piece'] = 'Random'
    return render_template('base.html', stuff=stuff)

Basic template, without variables, still causes error.

<html>
<head>
    <title>Something</title>
</head>
    <body>Something else</body>
</html>
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
  File "/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File "/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
  File "/env/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/frontend/controller.py", line 12, in index
return render_template('base.html', stuff=stuff)
  File "/env/lib/python3.4/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 "/env/lib/python3.4/site-packages/jinja2/environment.py", line 851, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
  File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
  File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 786, in _load_template
template = self.loader.load(self, name, globals)
  File "/env/lib/python3.4/site-packages/jinja2/loaders.py", line 125, in load
code = environment.compile(source, name, filename)
  File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 555, in compile
defer_init=defer_init)
  File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 515, in _generate
return generate(source, self, name, filename, defer_init=defer_init)
  File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 62, in generate
generator.visit(node)
  File "/env/lib/python3.4/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
  File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 816, in visit_Template
self.blockvisit(node.body, frame)
  File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 492, in blockvisit
self.visit(node, frame)
  File "/env/lib/python3.4/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
  File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 1250, in visit_Output
const = child.as_const(frame.eval_ctx)
  File "/env/lib/python3.4/site-packages/jinja2/nodes.py", line 467, in as_const
return Markup(self.data)
AttributeError: 'TemplateData' object has no attribute 'data'

Aucun commentaire:

Enregistrer un commentaire