使用Flask框架从python将变量传递到渲染模板中的HTML不起作用?

问题描述 投票:0回答:1

app.py

from flask import Flask, render_template
import random

app = Flask(__name__)


@app.route("/")
def index():
    number = random.randint(0, 10)
    return render_template("index.html", value='number')

templates / index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Random Number</title>
</head>

<body>
    Your random number is {{ value }}.
</body>

</html>

网页结果

您的随机数是数字。

不是用实际数字替换数字。请帮助!

顺便说一句,我正在PyCharm中对此进行编码

python html function variables flask
1个回答
0
投票

替换

return render_template("index.html", value='number')

with

return render_template("index.html", value=number)

您需要发送变量而不是字符串。如果您使用value ='number',则value是一个字符串,该字符串为'number'或引号内的任何内容。如果要使用'number'变量的值,请使用不带引号的变量。这将确保您传递数字变量的值。

© www.soinside.com 2019 - 2024. All rights reserved.