解決python訪問報錯:jinja2.exceptions.TemplateNotFound:index.html
背景
項目目錄結構
test/
–index.html # 主頁
–app.py
–count.json # 存儲訪問數(shù)據(jù)文件
三個文件均在同一級。
文件內容
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:
# 讀取計數(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>網站首頁</h1>
<p>Hello World! 該頁面已被訪問<b>{{ count }}</b>次。</p>
</body>
</html>
運行報錯:
jinja2.exceptions.TemplateNotFound
jinja2.exceptions.TemplateNotFound: index.html
Traceback (most recent call last)

解決
render_template方法會在同級templates目錄下查找。
調整index.html文件位置解決。
調整后目錄結構:
test/ --templates/ index.html # 主頁 --app.py --count.json # 存儲訪問數(shù)據(jù)文件
重啟后,成功訪問。

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python?ArcPy實現(xiàn)批量拼接長時間序列柵格圖像
這篇文章主要介紹了如何基于Python中ArcPy模塊,對大量不同時相的柵格遙感影像按照其成像時間依次執(zhí)行批量拼接的方法,感興趣的可以了解一下2023-03-03
Pycharm中運行程序在Python?console中執(zhí)行,不是直接Run問題
這篇文章主要介紹了Pycharm中運行程序在Python?console中執(zhí)行,不是直接Run問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Python plt.boxplot函數(shù)及其參數(shù)使用小結
plt.boxplot函數(shù)用于繪制箱線圖,本文介紹了Python plt.boxplot函數(shù)及其參數(shù)使用小結,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2024-02-02
Pytorch自定義CNN網絡實現(xiàn)貓狗分類詳解過程
PyTorch是一個開源的Python機器學習庫,基于Torch,用于自然語言處理等應用程序。它不僅能夠實現(xiàn)強大的GPU加速,同時還支持動態(tài)神經網絡。本文將介紹PyTorch自定義CNN網絡實現(xiàn)貓狗分類,感興趣的可以學習一下2022-12-12
jupyter notebook tensorflow打印device信息實例
這篇文章主要介紹了jupyter notebook tensorflow打印device信息實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python使用tarfile模塊實現(xiàn)免費壓縮解壓
Python自帶的tarfile模塊可以方便讀取tar歸檔文件,厲害的是可以處理使用gzip和bz2壓縮歸檔文件tar.gz和tar.bz2,這篇文章主要介紹了Python使用tarfile模塊實現(xiàn)免費壓縮解壓,需要的朋友可以參考下2024-03-03

