Forums

Flask app suddenly stopped working and errors with any passed variable to template

I have created a small app to test the error is not with my deployment which suddenly stopped working. the result is the same.

2021-06-17 11:55:44,513: Exception on / [GET]
Traceback (most recent call last):
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/austinsoft/mysite/anglersdb.py", line 7, in homepage
    return render_template("index.html", result={"title": "test0"})
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/flask/templating.py", line 134, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/environment.py", line 1068, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/environment.py", line 997, in get_template
    return self._load_template(name, globals)
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/environment.py", line 958, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/loaders.py", line 137, in load
    code = environment.compile(source, name, filename)
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/environment.py", line 749, in compile
    source = self._parse(source, name, filename)
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/environment.py", line 606, in _parse
    return Parser(self, source, name, filename).parse()
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 1038, in parse
    result = nodes.Template(self.subparse(), lineno=1)
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 1012, in subparse
    add_data(self.parse_tuple(with_condexpr=True))
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 725, in parse_tuple
    args.append(parse())
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 523, in parse_expression
    return self.parse_condexpr()
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 528, in parse_condexpr
    expr1 = self.parse_or()
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 543, in parse_or
    left = self.parse_and()
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 552, in parse_and
    left = self.parse_not()
  File "/home/austinsoft/.virtualenvs/myvirtualenv/lib/python3.6/site-packages/jinja2/parser.py", line 560, in parse_not
    if self.stream.current.test("name:not"):
AttributeError: 'Token' object has no attribute 'test'

[edit by admin: formatting]

What are the contents of the template? What happens if you don't pass in any context variables at all?

template is

<html>
    <head>
        <title>Sample app</title>
    </head>
    <body>
        <h1>{{ result.title }}</h1>
        <p><a href="/docs">Docs</a></p>
        <p><a href="/about">About</a></p>
        <p><a href="/">Home</a></p>
    </body>
</html>

simplified app is:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def homepage():
    return render_template("index.html", result={"title": "test0"})

@app.route("/docs")
def docs():
    return render_template("index.html", result={"title": "test1"})

@app.route("/about")
def about():
    return render_template("index.html", result={"title": "test2"})

if __name__ == "__main__":
    app.run()

if I do not pass any variables and remove the {{ result.title }} it renders correctly if I do return render_template("index.html", title="test") it gives the same error

[edit by admin: formatting]

If you're passing in a dict for the result context variable, should you not be using

<h1>{{ result['title'] }}</h1>

...in the template?

Same error with <h1>{{ result['title'] }}</h1> but thanks for reply

End result is always AttributeError: 'Token' object has no attribute 'test' What does this even mean?

Forgot to add my original app was working just fine until it was not LOL It works fine on my local machine using the localhost:5000 Flask server It is running in vutualenv, I have tried deleting the virtual folder and creating it again, but still the same error as soon as I try to pass even the simplest variable to jinja

Looks like problem other people encountered and solved.

Hi, yep that looks like the problem. I have python 3.9 on my local machine and it works fine. cannot alter that on pythonanywhere, so looks like i will have to regress to an earlier version of jinjs. Thanks for the reply. Mike.

Actually you can use Python 3.9 now -- you'd need to change your system image to the newest one ("glastonbury"), which is deployed, but not fully released yet. See: https://help.pythonanywhere.com/pages/ChangingSystemImage/ (you won't see "glastonbury" mentioned there yet, but it will be shown on your Account page).

Hi, thank you. moving to a different image ( 3.9 ) fixed it. Mike.

Excellent, thanks for confirming that!