Written by Sümeyye Sever (notes I took while learning Python)


index.html:

<!DOCTYPE html>
<html lang="en">
	<head>
    <meta charset="UTF-8">
    <title>Title</title>
	</head>
	<body>
		<form action="/login" method="post">
		  <label for="name">Name:</label><br>
		  <input type="text" id="name" name="username"><br>
		  <label for="password">Password:</label><br>
		  <input type="password" id="password" name="password"><br><br>
		  <input type="submit" value="OK">
		</form>
	</body>
</html>
<form action="/login" method="post">

How It Works:

  1. A user fills in their Name and Password fields.
  2. When the user clicks the OK button, the form sends the data to the server(main.py) using the POST method at the /login endpoint.
  3. The server processes the form data (name=”username” and name=”password”).

main.py:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/login", methods=["POST"])
def log_in():
    if request.method == 'POST':
        name = request.form["username"]
        password = request.form["password"]
        return f"<h1>Hello {name}. This is your password: {password}"

if __name__ == "__main__":
    app.run(debug=True)

Login route code breakdown:

How It Works: