HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Random Password Generator</title> </head> <body> <form action="/generate_password" method="POST"> <label for="password">Generated Password:</label> <input type="text" id="password" name="password" readonly> <button type="submit">Generate Password</button> </form> </body> </html>
Python Flask:
from flask import Flask, render_template, request import random import string app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/generate_password', methods=['POST']) def generate_password(): password_length = 12 # You can change the length of the generated password here generated_password = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=password_length)) return render_template('index.html', generated_password=generated_password) if __name__ == '__main__': app.run(debug=True)
Try to answer about this code:
What does the HTML form in the code allow users to do?
Display a static password on the webpage
Submit a request to generate a random password
Change the password of the Flask application
Redirect to another webpage
Which Flask route is responsible for generating the random password?
/
/generate_password
/get_password
/password
What does the random.choices() function in Python do in the code?
Chooses a random element from a list
Generates a sequence of random characters
Generates a random integer within a range
Selects a random string from a dictionary
What type of HTML input is used to display the generated password?
<text>
<input>
<password>
<readonly>