使用 Flask 无法显示 html
Can't show html using Flask
所以我一直在努力适应 python 中的 Flash,但我遇到了一个问题。我希望在浏览器中插入 http://localhost:5000/
时显示 html 页面。我已经尝试了多种方法来做到这一点,比如使用 render_template()
但是 returns 我 jinja2.exceptions.TemplateNotFound: index.html
。我也试过一个简单的 return redirect()
,但它会抛出一些东西说地址没有被识别或理解。当我尝试使用 url_for()
时,它抛出 404 - 未找到。我真的不知道如何解决这个问题。
# htppserver.py
import flask
import threading
app = Flask(__name__, template_folder="Dashboard/website")
@app.route("/site", methods=["GET"])
@app.route("/", methods=["GET"])
def get_site():
return render_template("index.html")
x = threading.Thread(target=app.run)
x.start()
目前我的目录系统看起来像这样
main_folder # This is the working directory accordingly to os.getcwd()
├──cogs
│ └──httpserver.py # Source code is here
└──Dashboard
└website
├──...
├──index.html # This is the file I want to show
└──...
谢谢
将您的 html 文件放入名为“templates”的文件夹中,该文件夹与服务 python 的文件位于同一目录中
所以我一直在努力适应 python 中的 Flash,但我遇到了一个问题。我希望在浏览器中插入 http://localhost:5000/
时显示 html 页面。我已经尝试了多种方法来做到这一点,比如使用 render_template()
但是 returns 我 jinja2.exceptions.TemplateNotFound: index.html
。我也试过一个简单的 return redirect()
,但它会抛出一些东西说地址没有被识别或理解。当我尝试使用 url_for()
时,它抛出 404 - 未找到。我真的不知道如何解决这个问题。
# htppserver.py
import flask
import threading
app = Flask(__name__, template_folder="Dashboard/website")
@app.route("/site", methods=["GET"])
@app.route("/", methods=["GET"])
def get_site():
return render_template("index.html")
x = threading.Thread(target=app.run)
x.start()
目前我的目录系统看起来像这样
main_folder # This is the working directory accordingly to os.getcwd()
├──cogs
│ └──httpserver.py # Source code is here
└──Dashboard
└website
├──...
├──index.html # This is the file I want to show
└──...
谢谢
将您的 html 文件放入名为“templates”的文件夹中,该文件夹与服务 python 的文件位于同一目录中