简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM