Python 为什么不是';t我的html代码输出变量';使用Flask渲染模板时的s值?

Python 为什么不是';t我的html代码输出变量';使用Flask渲染模板时的s值?,python,html,flask,jinja2,Python,Html,Flask,Jinja2,我有一个main.py文件和一个templates文件夹,在templates文件夹中有math.html。它只是一个基本的html文件。在main.py中,我正在呈现名为math.html的模板。注意,我使用的是烧瓶 这是我的main.py: from flask import Flask, render_template import random app = Flask(__name__) @app.route('/') def home(): return render_te

我有一个main.py文件和一个templates文件夹,在templates文件夹中有math.html。它只是一个基本的html文件。在main.py中,我正在呈现名为math.html的模板。注意,我使用的是烧瓶

这是我的main.py:

from flask import Flask, render_template
import random

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/math-game')
def game():  # This is the function which renders math.html
    num1 = random.randint(0, 100)
    num2 = random.randint(0, 100)

    return render_template('math.html', num1=num1, num2=num2)
而在my math.html中,我有以下代码:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Math Game</title>
</head>
<body>
<center><h1>Math Game</h1></center>

<h2>What is {{ num1 }} + {{ num2 }}?</h2>
</body>
</html>

数学游戏
数学游戏
什么是{{num1}}+{{num2}}?
当我在我的网站中检查输出时,它只显示“什么是{{num1}}+{{num2}}?”。为什么它不从main.py获取num1和num2的值并在这里显示,而不是只显示上面的内容

请帮忙

谢谢

试试看


不要打开math-game.html,因为这将加载.html.file,并且它不会读取{{}

愚蠢的问题,但是如何运行应用程序?你在哪里查看页面?我使用本地主机端口127。。。math.htmly你没有启动应用程序吗?类似python app.py的东西?如果您只是查看页面而没有flask提供它,您当然不会看到呈现的模板。是的,您应该使用
flask run
运行此页面并加载URL
http://localhost:5000/math-浏览器中的游戏
。不直接在浏览器中打开模板文件,这听起来像是在做。