A login form lets users enter their username and password to access a website securely. Verification checks if the entered details are correct.
Login form and verification in 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.
@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')
@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')
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.
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)
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.
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.
Practice
Solution
Step 1: Understand the role of a login form
A login form is designed to collect user credentials like username and password.Step 2: Identify the correct purpose in Flask context
In Flask, the login form collects these details to verify user identity before granting access.Final Answer:
To collect username and password from users -> Option CQuick Check:
Login form = collect credentials [OK]
- Confusing login form with other features like email or images
- Thinking login form creates database tables
- Assuming login form sends emails automatically
Solution
Step 1: Identify the correct HTTP method for showing a form
Login forms are usually shown with GET requests, so methods=['GET'] is correct.Step 2: Check the route and function return
The route '/login' with methods=['GET'] returns the login.html template to display the form.Final Answer:
app.route('/login', methods=['GET'])\ndef login():\n return render_template('login.html') -> Option AQuick Check:
Show form = GET method [OK]
- Using POST method to show the form instead of GET
- Not specifying methods list defaults to GET but unclear
- Using DELETE method which is invalid for forms
from flask import Flask, request, render_template_string
app = Flask(__name__)
@app.route('/login', methods=['POST'])
def login():
user = request.form.get('username')
pwd = request.form.get('password')
if user == 'admin' and pwd == '1234':
return 'Login successful'
else:
return 'Login failed'Solution
Step 1: Check the submitted username and password
The code compares user to 'admin' and pwd to '1234'. Both match the submitted values.Step 2: Determine the returned response
Since both match, the function returns 'Login successful'.Final Answer:
Login successful -> Option AQuick Check:
Correct credentials = Login successful [OK]
- Confusing POST and GET methods for form data
- Assuming wrong output when credentials match
- Ignoring case sensitivity in username/password
@app.route('/login', methods=['POST'])
def login():
username = request.form['username']
password = request.form['password']
if username = 'user' and password = 'pass':
return 'Welcome!'
else:
return 'Invalid credentials'Solution
Step 1: Check the if condition syntax
The code uses single '=' which is assignment, not comparison. It should be '==' to compare values.Step 2: Verify other parts of the code
The methods argument is present, request.form is correctly used, and return statements exist.Final Answer:
Using '=' instead of '==' in the if condition -> Option DQuick Check:
Comparison needs '==' not '=' [OK]
- Using '=' instead of '==' in conditions
- Forgetting to specify POST method in route
- Using request.args for POST form data
Solution
Step 1: Understand where to store attempt count
Using session storage per user is best to track attempts individually without global conflicts.Step 2: Implement logic to check attempts before verifying credentials
Check if attempts exceed 3 before allowing login verification to prevent further tries.Step 3: Reset attempt count on successful login
Resetting attempts after success avoids locking out valid users.Final Answer:
Store attempt count in session and check before verifying credentials -> Option BQuick Check:
Session tracks attempts per user [OK]
- Using global variables causing shared count for all users
- Resetting attempts only after success but not checking before login
- Ignoring attempt limits and allowing unlimited tries
Ready to prove it?
Pass the test and this pattern moves unknown
