A login form lets users enter their username and password to access a website securely. Verification checks if the entered details are correct.
0
0
Login form and verification in Flask
Introduction
When you want to restrict parts of your website to registered users only.
When you need to personalize user experience after they sign in.
When you want to protect sensitive information behind a secure login.
When you want to track user activity after they log in.
Syntax
Flask
from flask import Flask, render_template, request, redirect, url_for app = Flask(__name__) @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] # Verify username and password here if username == 'user' and password == 'pass': return redirect(url_for('welcome')) else: return 'Login Failed' return render_template('login.html') @app.route('/welcome') def welcome(): return 'Welcome!'
The @app.route decorator defines the URL path and allowed methods.
request.form gets data submitted from the form.
Examples
Basic login route that checks username and password and returns a message.
Flask
@app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] # Check credentials if username == 'admin' and password == '1234': return 'Logged in!' else: return 'Wrong credentials' return render_template('login.html')
Checks if username and password are entered before proceeding.
Flask
@app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') if username and password: # Add real verification here return redirect(url_for('dashboard')) else: return 'Please enter username and password' return render_template('login.html')
Sample Program
This Flask app shows a login form. When the user submits the form, it checks if the username is 'admin' and password is 'secret'. If correct, it redirects to a welcome page. If wrong, it shows an error message on the same page.
Flask
from flask import Flask, render_template_string, request, redirect, url_for app = Flask(__name__) login_form = ''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login</title> </head> <body> <h2>Login Form</h2> <form method="POST" action="/login"> <label for="username">Username:</label><br> <input type="text" id="username" name="username" required><br><br> <label for="password">Password:</label><br> <input type="password" id="password" name="password" required><br><br> <button type="submit">Login</button> </form> {% if error %} <p style="color:red;">{{ error }}</p> {% endif %} </body> </html> ''' @app.route('/login', methods=['GET', 'POST']) def login(): error = None if request.method == 'POST': username = request.form['username'] password = request.form['password'] # Simple verification: username is 'admin' and password is 'secret' if username == 'admin' and password == 'secret': return redirect(url_for('welcome')) else: error = 'Invalid username or password' return render_template_string(login_form, error=error) @app.route('/welcome') def welcome(): return '<h1>Welcome, admin!</h1>' if __name__ == '__main__': app.run(debug=True)
OutputSuccess
Important Notes
Never store passwords as plain text in real apps; use hashing.
Use HTTPS to keep login data safe during transmission.
Flask's render_template_string is used here for simplicity; normally use separate HTML files.
Summary
A login form collects username and password from users.
Verification checks if the entered details match expected values.
Flask routes handle showing the form and processing the login data.