-2

I was trying to create a simple form using html and python, and I found the flask library helpful for that, but it seems I did something wrong.

file: 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>

file: 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)

Using my vscode terminal, I typed python app.py and got a massive error log:

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 -

How can I fix this problem?

1 Answers1

0

Withing your working repo , Create a folder named templates and copy your html files to it .

ELOUTMADI
  • 59
  • 1
  • 6