这里是我的Python和HTML代码,在生日当天显示“生日快乐”,在其余日子显示“否”。
Application.py
import datetime
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def ind():
now = datetime.datetime.now()
birthday = now.month == 9 and now.day == 16
return render_template("ind.html", birthday = birthday)ind.html
<!doctype html>
<html>
<head>
<title>Is it the birthday?</title>
<style>
body{
text-align: center;
}
</style>
</head>
<body>
{%if birthday%}
<h1>Happy Birthday</h1>
{%else%}
<h1>No</h1>
{%endif%}
</body>
</html>我所犯的错误:
* Serving Flask app "application"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2020-05-02 10:42:47,258] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/heisenberg/my_flask_app/application.py", line 10, in ind
return render_template("ind.html", birthday = birthday)
File "/usr/lib/python3/dist-packages/flask/templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/lib/python3/dist-packages/jinja2/loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/lib/python3/dist-packages/flask/templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "/usr/lib/python3/dist-packages/flask/templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: ind.html
127.0.0.1 - - [02/May/2020 10:42:47] "GET / HTTP/1.1" 500 -我运行了以下命令:
创建虚拟环境的
源venv/bin/activate
导出FLASK_APP=application.py
烧瓶运行
模板目录结构:
my_flask_app/venv/lib64/python3.6/site-packages/flaskMain.py的目录结构:
my_flask_app/venv/lib64/python3.6/site-packages/flask请让我知道我的错误。
提前谢谢。
发布于 2020-05-02 08:48:43
这个问题在这里已经有了答案:Flask Template Not found
由default搜索html contents文件夹,默认情况下位于Application.py的根目录下。如果它没有得到templates文件夹,那么它将显示错误。与默认情况下让烧瓶搜索它不同,应该显式地声明它为。
app = Flask(__name__,
template_folder='templates', # your templates folder name goes here
static_folder='static', # your static folder name goes here
)为了避免Folder/File Not found,我建议在这些参数中给出full absolute path。您还可以检查是否有类似bug的template和templates文件夹命名。
小贴士:,正如你提到的,Long Errors。当代码中存在大量依赖项时,就会出现长错误消息。这意味着,如果你错过了一件事,剩下的也会爆炸!尝试检查py文件中的message/line number,而不是3rd party packages message/line number。检查最后几行错误在80%的时间内有效,因为这一行启动了错误。
发布于 2020-05-02 06:55:54
您应该创建一个模板目录,在其中存储html文件。
模板目录必须与python文件的级别相同。
-templates
-ind.html
-main.pyhttps://stackoverflow.com/questions/61554836
复制相似问题