使用flask的Html和python形式[重复]

问题描述 投票:0回答:1

我试图使用 html 和 python 创建一个简单的表单,我发现 Flask 库对此很有帮助,但似乎我做错了什么。

文件:

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)

使用我的 vscode 终端,输入

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 -

如何解决这个问题?

python html flask
1个回答
0
投票

在您的工作存储库中,创建一个名为 templates 的文件夹并将 html 文件复制到其中。

© www.soinside.com 2019 - 2024. All rights reserved.