亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

背景

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

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

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

文件內(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>首頁(yè)</title>
</head>
<body>
    <h1>網(wǎng)站首頁(yè)</h1>
    <p>Hello World! 該頁(yè)面已被訪問(wèn)<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方法會(huì)在同級(jí)templates目錄下查找。

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

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

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

重啟后,成功訪問(wèn)。

總結(jié)

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

相關(guān)文章

最新評(píng)論