首页 > 解决方案 > 通过 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 -

我已经运行了以下命令:

  1. 创建虚拟环境:

    源 venv/bin/激活

  2. 运行我的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

请让我意识到我的错误。

提前致谢。

标签: flaskpython-3.6

解决方案


您应该创建一个模板目录来存储您的 html 文件

模板目录必须与 python 文件处于同一级别

-templates
    -ind.html
-main.py

推荐阅读