解決python訪問報錯:jinja2.exceptions.TemplateNotFound:index.html
更新時間:2023年12月01日 09:15:07 作者:三省同學
這篇文章主要介紹了解決python訪問報錯:jinja2.exceptions.TemplateNotFound:index.html,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
背景
項目目錄結構
test/
–index.html # 主頁
–app.py
–count.json # 存儲訪問數據文件
三個文件均在同一級。
文件內容
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:
# 讀取計數文件并+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 # 存儲訪問數據文件
重啟后,成功訪問。

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Pycharm中運行程序在Python?console中執(zhí)行,不是直接Run問題
這篇文章主要介紹了Pycharm中運行程序在Python?console中執(zhí)行,不是直接Run問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
jupyter notebook tensorflow打印device信息實例
這篇文章主要介紹了jupyter notebook tensorflow打印device信息實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

