Flask实现HTML登录表单遇TemplateNotFound错误的解决咨询
Flask登录表单TemplateNotFound错误解决方案
问题描述
我用Flask和HTML制作登录表单,代码如下:
login.html
<!DOCTYPE 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>Document</title> </head> <body> <!-- HTML template for the login form --> <form method="post" action="/login"> <label for="username">Username:</label><br> <input type="text" id="username" name="username"><br> <label for="password">Password:</label><br> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
app.py
from flask import Flask, render_template, request app = Flask(__name__) @app.route('/') def home(): return render_template('login.html') @app.route('/login', methods=['POST']) def login(): # Get the username and password from the form submission username = request.form['username'] password = request.form['password'] # Validate the username and password if username == 'admin' and password == 'secret': return 'Logged in successfully' else: return 'Invalid username or password' if __name__ == '__main__': app.run(debug=True)
运行python app.py后出现错误:
127.0.0.1 - - [30/Dec/2022 01:22:00] "GET / HTTP/1.1" 500 - Traceback (most recent call last): File "C:\Users\itzfr\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 2548, in __call__ return self.wsgi_app(environ, start_response) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\itzfr\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 2528, in wsgi_app response = self.handle_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\itzfr\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 2525, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\itzfr\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1822, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\itzfr\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1820, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\itzfr\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1796, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\coding\chatgpt-testing\app.py", line 7, in home return render_template('login.html') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: login.html 127.0.0.1 - - [30/Dec/2022 01:22:00] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [30/Dec/2022 01:22:00] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [30/Dec/2022 01:22:00] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [30/Dec/2022 01:22:01] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 304 - 127.0.0.1 - - [30/Dec/2022 01:22:02] "GET /null HTTP/1.1" 404 - 127.0.0.1 - - [30/Dec/2022 01:22:02] "GET /null HTTP/1.1" 404 -
解决方案
错误原因
Flask框架默认会在项目根目录下的**templates文件夹**中查找模板文件,你的login.html直接放在根目录,不符合Flask的模板路径规则,因此抛出TemplateNotFound错误。
解决步骤
- 创建
templates文件夹:在与app.py同级的目录下,新建一个名为templates的文件夹。 - 移动模板文件:将
login.html剪切到刚创建的templates文件夹内。 - 重启服务:停止当前运行的Flask服务,重新执行
python app.py。
验证
重启后访问http://127.0.0.1:5000,即可正常显示登录表单;输入用户名admin、密码secret提交后会返回Logged in successfully,输入其他内容则返回Invalid username or password。
内容的提问来源于stack exchange,提问作者paulos
相关产品推荐
相关产品推荐

