Roamex
Roamex

Reputation: 31

Python code in Flask's HTML curly braces don't work

Whenever I try to write code in curly braces it doesn't work, shows error 404 page not found. I copied the code from 9:48 in this video.

from flask import Flask, redirect, url_for, render_template

app = Flask(__name__)


@app.route("/<name>")
def home(name):
    return render_template("index.html")


if __name__ == "__main__":
    app.run()
<!DOCTYPE html>
<html>
  <head>
    <title>Home Page</title>
  </head>
  <body>
    <h1>Home Page!</h1>
    {% for x in range(10) %}
     {% if x % 2 == 1 %}
        <p>x</p>
      {% endif %}
     {% endfor %}
  </body>
</html>

Also this is printed in the terminal below some other stuff which I think is why this is happening.

jinja2.exceptions.UndefinedError: 'x' is undefined

Upvotes: 0

Views: 866

Answers (1)

Roamex
Roamex

Reputation: 31

Ok so the problem was that I had several different terminals open and it seemed that I used an older version, where an error was in the code. To fix this, simply kill all of the active terminal instances and then run your script by opening a new terminal with VS Code -> Terminal -> New terminal or with Ctrl+Shift+`.

How to kill active terminal instance

Upvotes: 1

Related Questions