Html和python表单使用 flask [复制]

fquxozlt  于 2023-01-03  发布在  Python
关注(0)|答案(1)|浏览(116)
    • 此问题在此处已有答案**:

(13个答案)
4天前关闭。
我试图用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 -

如何解决此问题?

lbsnaicq

lbsnaicq1#

使用您的工作repo,创建一个名为templates的文件夹,并将您的html文件复制到其中。

相关问题