J2112O
J2112O

Reputation: 635

Flask context variable not available in template

New to Flask and trying to work on a tutorial but running into some problems with the context variable from render_template() method.

Here is app.py:

from flask import Flask, render_template, url_for
app = Flask(__name__)

posts = [
    {
        'author': 'Some Author',
        'title': 'Blog Post 1',
        'content': 'First blog post',
        'date_posted': 'April 21, 2018'
    },
    {
        'author': 'Another Author',
        'title': 'Blog Post 2',
        'content': 'Second blog post',
        'date_posted': 'May 21, 2013'
    }
]


@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html', posts=posts)


@app.route("/about")
def about():
    return render_template('about.html')


if __name__ == '__main__':
    app.run(debug=True)

And this is my home.html template:

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
</head>
<body>
    { % for post in posts % }
        <h4>{{ post.title }}</h4>
        <p>By {{ post.author }} on {{ post.date_posted }}</p>
        <p>{{ post.content }}</p>
    { % endfor % }
</body>
</html>

But the above isn't working. In browser at top of the page I get,

jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: 'post' is undefined

Here is the traceback from the terminal as well:

File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/bigdaddy/Sites/Flask_Blog/app.py", line 25, in home
    return render_template('home.html', some_list=posts)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/templating.py", line 135, in render_template
    context, ctx.app)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/flask/templating.py", line 117, in _render
    rv = template.render(context)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/home/bigdaddy/Sites/Flask_Blog/templates/home.html", line 8, in top-level template code
    <h4>{{ post.title }}</h4>
  File "/home/bigdaddy/Sites/Flask_Blog/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
    return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'post' is undefined

Any help would be greatly appreciated. Using python3.6 in a virtualenv on Xubuntu Linux 16.04 if that helps. Thanks

Upvotes: 1

Views: 3912

Answers (1)

Nevelina A.
Nevelina A.

Reputation: 166

Jinja isn't detecting your for loop because you have put a space between { and % and Jinja is looking for the specific pair {%. Right now Jinja is completely ignoring the { % for post in posts % } line, treating as part of the HTML it doesn't have to do anything with and moving onto the next one, where it's failing to find post.

The following fixes the start and end of the loop:

<body>
    {% for post in posts %}
        <h4>{{ post.title }}</h4>
        <p>By {{ post.author }} on {{ post.date_posted }}</p>
        <p>{{ post.content }}</p>
    {% endfor %}
</body>

Upvotes: 5

Related Questions