最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決python訪問報(bào)錯(cuò):jinja2.exceptions.TemplateNotFound:index.html

 更新時(shí)間:2023年12月01日 09:15:07   作者:三省同學(xué)  
這篇文章主要介紹了解決python訪問報(bào)錯(cuò):jinja2.exceptions.TemplateNotFound:index.html,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

背景

項(xiàng)目目錄結(jié)構(gòu)

test/
–index.html # 主頁
–app.py
–count.json # 存儲訪問數(shù)據(jù)文件

三個(gè)文件均在同一級。

文件內(nèi)容

app.py

from flask import Flask
from flask import render_template
from json import load, dump

app = Flask(__name__)
app.config["SECRET_KEY"] = '123456'

@app.route("/")
def index():
    with open("count.json") as f:
        # 讀取計(jì)數(shù)文件并+1回寫
        people = load(f) + 1
        with open("count.json", "w") as f:
            dump(people, f)
    return render_template("index.html", people=str(people))

if __name__ == "__main__":
    app.run(host="127.0.0.1", port="8000", debug=True)

count.josn

0

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
</head>
<body>
    <h1>網(wǎng)站首頁</h1>
    <p>Hello World! 該頁面已被訪問<b>{{ count }}</b>次。</p>
</body>
</html>

運(yùn)行報(bào)錯(cuò):

jinja2.exceptions.TemplateNotFound

jinja2.exceptions.TemplateNotFound: index.html
Traceback (most recent call last)

解決

render_template方法會在同級templates目錄下查找。

調(diào)整index.html文件位置解決。

調(diào)整后目錄結(jié)構(gòu):

test/
--templates/
	index.html # 主頁
--app.py 	
--count.json # 存儲訪問數(shù)據(jù)文件

重啟后,成功訪問。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

永康市| 天等县| 孝感市| 千阳县| 杭锦旗| 灌阳县| 建昌县| 慈溪市| 页游| 根河市| 诏安县| 乌审旗| 铁力市| 儋州市| 宿松县| 永嘉县| 略阳县| 安龙县| 余姚市| 八宿县| 山西省| 益阳市| 桓台县| 得荣县| 湟源县| 湾仔区| 寿阳县| 阿鲁科尔沁旗| 赫章县| 剑川县| 陆良县| 卓尼县| 宿迁市| 东港市| 大兴区| 成安县| 穆棱市| 隆子县| 广州市| 定西市| 浦县|