flask - 通过 Flask 运行 Python 文件时出现长错误
问题描述
这是我的 Python 和 HTML 代码,用于在生日当天显示“生日快乐”,在其余日子显示“不”。
应用程序.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/激活
运行我的python文件。
导出 FLASK_APP=application.py
烧瓶运行
模板目录结构:
my_flask_app/venv/lib64/python3.6/site-packages/flask
main.py的目录结构:
my_flask_app/venv/lib64/python3.6/site-packages/flask
请让我意识到我的错误。
提前致谢。
解决方案
您应该创建一个模板目录来存储您的 html 文件
模板目录必须与 python 文件处于同一级别
-templates
-ind.html
-main.py
推荐阅读
- python - 将 pandas.get_dummies 用于返回 3 列的二元特征时出现问题,其中一列带有表格
- node.js - 我想使用 Alamofire 在我的 swift 代码上使用登录 API (node.js)
- angular - Angular Material Sidenav 渲染但未显示
- dataframe - 从 spark 数据框中的 Wrappedarray 中提取元素
- python - 在 python 中使用 CSV 模块在列中写入数据
- angular-reactive-forms - Angular8 反应式表单验证器和异步验证器
- kotlin - 如何等待函数调用?
- android - Android studio 可运行,线程
- typescript - 可选参数影响返回类型
- python-3.x - Python 中的 LMAX 破坏者