500 Internal Server Error

我是flask的小白,最近在嘗試架網站,目前情況是我用ngrok把他架上去了,運行也可以運行,但一旦我想在python引入html他就給我跳Internal Server Error



from flask import Flask, request, render_template


app = Flask(__name__, static_folder="public", static_url_path="/")

# 處理路徑 / 的對應函市
@app.route("/")
def index():
    return render_template("123.html")

app.run()

html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>這是標題title>
head>

<body>
    <h3>網頁的主畫面h3>
body>

html>

[2023-04-09 10:23:17,502] ERROR in app: Exception on / [GET]      
Traceback (most recent call last):
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 2528, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1799, in dispatch_request    
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\python\flask\app.py", line 10, in index
    return render_template("123.html")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\templating.py", line 146, in render_template
    template = app.jinja_env.get_or_select_template(template_name_or_list)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\jinja2\environment.py", line 1081, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\jinja2\environment.py", line 1010, in get_template
    return self._load_template(name, globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\jinja2\environment.py", line 969, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\jinja2\loaders.py", line 126, in load
    source, filename, uptodate = self.get_source(environment, name)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\templating.py", line 62, in get_source     
    return self._get_source_fast(environment, template)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\johnson\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\templating.py", line 98, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: 123.html
127.0.0.1 - - [09/Apr/2023 10:23:17] "GET / HTTP/1.1" 500 -


有用的话,请采纳哇,这对我很重要!!!
在 Flask 中,若引用了模板文件,必须将模板文件存放在应用的模板文件夹中,模板文件夹默认为应用目录中的 templates 文件夹。在的代码中没有指定模板文件夹,因此 Flask 默认会去找 templates 文件夹。可以在应用中添加以下代码,将模板文件夹设置为当前目录:

pythonCopy codeimport os
app = Flask(__name__, static_folder="public", static_url_path="/", template_folder=os.getcwd())
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^