FireShot Capture 137 - 100 Days of Code_ The Complete Python Pro Bootcamp for 2023 - Udemy_ - www.udemy.com.png

Using Jinja to Produce Dynamic HTML Pages

先基本起手式練一下,可以從教材下載

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello World!"

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

確定可以執行的下一步,請產生一個html file 並使用render

此檔請放置名為”templates”的資料夾內

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>my website</title>
</head>
<body>
    <h1>hello day57 frist challenge</h1>
</body>
</html>

server.py修改


[email protected]('/')
def home():
    return "Hello World!"~~

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

ex1 修改index.html 部份內容

<body>
    <h1>hello day57 frist challenge</h1>
    <h1> 5 * 6</h1>
    <h1>{{ 5 * 6}}</h1>

</body>
</html>