解决报错:jinja2.exceptions.TemplateNotFound: index.html

解决Flask渲染HTML模板时的TemplateNotFound错误

一、问题描述

(1)首先写了一个简单的登录账号密码的页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="">
    账号:
    <input type="text" name="name">
    <br>
    密码:
    <input type="password" name="password">
    <br>
    <input type="submit" name="submit">
</form>
</body>
</html>

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(2)然后利用flask进行form表单渲染,文件form表单

# -*- coding: utf-8 -*-
"""
Created on Sat Dec 25 00:34:23 2021
@author: 86493
"""
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/index')
def index():
    return render_template('index.html')
if __name__ == '__main__':
    app.run()

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

(3)发现报错:jinja2.exceptions.TemplateNotFound: index.html,即没找到我的html.py文件

二、解决方法:

首先要在form表单.py的同级目录下创建文件夹templates,然后把__init__.py和我们刚才写的html文件丢进去。
在这里插入图片描述
(1)如果在pycharm中就在左侧栏右键点击mark directory as的template folder,添加jinja2的模板。
(2)我是用webstorm软件的,则不用(1)的步骤,直接能识别到同级目录下的templateshtml文件,但是我一开始将文件夹名命名成template了,所以报错。

浏览器输入网址:
在这里插入图片描述
提交账号密码后,按F12,也说明这是一个GET请求:
在这里插入图片描述



山顶夕景

解决报错:jinja2.exceptions.TemplateNotFound: index.html

D:\python2\python3.exe D:\PythonProject\gx_day11\Web.py * Serving Flask app 'Web' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit 127.0.0.1 - - [23/Jul/2025 21:58:04] "GET / HTTP/1.1" 404 - [2025-07-23 21:58:11,619] ERROR in app: Exception on /show/info [GET] Traceback (most recent call last): File "D:\python2\Lib\site-packages\flask\app.py", line 1511, in wsgi_app response = self.full_dispatch_request() File "D:\python2\Lib\site-packages\flask\app.py", line 919, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\python2\Lib\site-packages\flask\app.py", line 917, in full_dispatch_request rv = self.dispatch_request() File "D:\python2\Lib\site-packages\flask\app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^ File "D:\PythonProject\gx_day11\Web.py", line 13, in index return render_template("show/info.html") File "D:\python2\Lib\site-packages\flask\templating.py", line 149, in render_template template = app.jinja_env.get_or_select_template(template_name_or_list) File "D:\python2\Lib\site-packages\jinja2\environment.py", line 1087, in get_or_select_template return self.get_template(template_name_or_list, parent, globals) ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\jinja2\environment.py", line 1016, in get_template return self._load_template(name, globals) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\jinja2\environment.py", line 975, in _load_template template = self.loader.load(self, name, self.make_globals(globals)) File "D:\python2\Lib\site-packages\jinja2\loaders.py", line 126, in load source, filename, uptodate = self.get_source(environment, name) ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\flask\templating.py", line 65, in get_source return self._get_source_fast(environment, template) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\flask\templating.py", line 99, in _get_source_fast raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: show/info.html 127.0.0.1 - - [23/Jul/2025 21:58:11] "GET /show/info HTTP/1.1" 500 - 127.0.0.1 - - [23/Jul/2025 21:58:38] "GET / HTTP/1.1" 404 - 127.0.0.1 - - [23/Jul/2025 21:58:38] "GET /favicon.ico HTTP/1.1" 404 - [2025-07-23 21:58:44,213] ERROR in app: Exception on /show/info [GET] Traceback (most recent call last): File "D:\python2\Lib\site-packages\flask\app.py", line 1511, in wsgi_app response = self.full_dispatch_request() File "D:\python2\Lib\site-packages\flask\app.py", line 919, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\python2\Lib\site-packages\flask\app.py", line 917, in full_dispatch_request rv = self.dispatch_request() File "D:\python2\Lib\site-packages\flask\app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^ File "D:\PythonProject\gx_day11\Web.py", line 13, in index return render_template("show/info.html") File "D:\python2\Lib\site-packages\flask\templating.py", line 149, in render_template template = app.jinja_env.get_or_select_template(template_name_or_list) File "D:\python2\Lib\site-packages\jinja2\environment.py", line 1087, in get_or_select_template return self.get_template(template_name_or_list, parent, globals) ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\jinja2\environment.py", line 1016, in get_template return self._load_template(name, globals) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\jinja2\environment.py", line 975, in _load_template template = self.loader.load(self, name, self.make_globals(globals)) File "D:\python2\Lib\site-packages\jinja2\loaders.py", line 126, in load source, filename, uptodate = self.get_source(environment, name) ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\flask\templating.py", line 65, in get_source return self._get_source_fast(environment, template) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\flask\templating.py", line 99, in _get_source_fast raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: show/info.html 127.0.0.1 - - [23/Jul/2025 21:58:44] "GET /show/info HTTP/1.1" 500 - [2025-07-23 21:58:45,321] ERROR in app: Exception on /show/info [GET] Traceback (most recent call last): File "D:\python2\Lib\site-packages\flask\app.py", line 1511, in wsgi_app response = self.full_dispatch_request() File "D:\python2\Lib\site-packages\flask\app.py", line 919, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\python2\Lib\site-packages\flask\app.py", line 917, in full_dispatch_request rv = self.dispatch_request() File "D:\python2\Lib\site-packages\flask\app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^ File "D:\PythonProject\gx_day11\Web.py", line 13, in index return render_template("show/info.html") File "D:\python2\Lib\site-packages\flask\templating.py", line 149, in render_template template = app.jinja_env.get_or_select_template(template_name_or_list) File "D:\python2\Lib\site-packages\jinja2\environment.py", line 1087, in get_or_select_template return self.get_template(template_name_or_list, parent, globals) ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\jinja2\environment.py", line 1016, in get_template return self._load_template(name, globals) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\jinja2\environment.py", line 975, in _load_template template = self.loader.load(self, name, self.make_globals(globals)) File "D:\python2\Lib\site-packages\jinja2\loaders.py", line 126, in load source, filename, uptodate = self.get_source(environment, name) ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\flask\templating.py", line 65, in get_source return self._get_source_fast(environment, template) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python2\Lib\site-packages\flask\templating.py", line 99, in _get_source_fast raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: show/info.html 127.0.0.1 - - [23/Jul/2025 21:58:45] "GET /show/info HTTP/1.1" 500 - 为什么浏览器找不到我的网站,pycharm是这么显示的,是什么原因怎么解决
07-24
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坦笑&&life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值