Progress Report 2
Progress Report 2
Introduction
This project is a simple login page built using Flask (a Python web framework) and MySQL as the
database. The application allows users to register, log in, and access a protected dashboard.
Technologies Used
• Python
• Flask
• MySQL
Flask login page using MySQL to store usernames and passwords. This project consists of:
• A login.html template
• A register.html template
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<style>
/* General Styles */
body {
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
/* Login Container */
.container {
background: white;
padding: 20px;
border-radius: 10px;
width: 300px;
text-align: center;
/* Headings */
h2 {
color: #333;
margin-bottom: 20px;
/* Input Fields */
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin: 8px 0;
border-radius: 5px;
font-size: 14px;
}
/* Button */
button {
width: 100%;
padding: 10px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
button:hover {
background-color: #0056b3;
/* Error Messages */
.error-message {
color: red;
font-size: 14px;
/* Register Link */
p{
margin-top: 10px;
a{
color: #007BFF;
text-decoration: none;
a:hover {
text-decoration: underline;
</style>
</head>
<body>
<div class="container">
<h2>Login</h2>
{% if messages %}
{% endfor %}
{% endif %}
{% endwith %}
<form method="POST">
<label>Username:</label>
<label>Password:</label>
</form>
</div>
</body>
</html>
Register page
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Register</h2>
{% if messages %}
{% endfor %}
{% endif %}
{% endwith %}
<form method="POST">
<label>Username:</label>
<label>Password:</label>
<br>
<button type="submit">Register</button>
</form>
</body>
</html>
App.py code
app = Flask(__name__,template_folder='template')
app.secret_key = "your_secret_key"
# MySQL Configuration
app.config["MYSQL_HOST"] = "localhost"
app.config["MYSQL_DB"] = "flask_login"
app.config["MYSQL_CURSORCLASS"] = "DictCursor"
mysql = MySQL(app)
bcrypt = Bcrypt(app)
# Home Route
@app.route("/")
def home():
if "user" in session:
return redirect("/login")
# Register Route
def register():
if request.method == "POST":
username = request.form["username"]
password = bcrypt.generate_password_hash(request.form["password"]).decode("utf-8")
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (username, password))
mysql.connection.commit()
cur.close()
return redirect("/login")
return render_template("register.html")
# Login Route
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
cur = mysql.connection.cursor()
user = cur.fetchone()
cur.close()
session["user"] = user["username"]
return redirect("/")
else:
return render_template("login.html")
# Logout Route
@app.route("/logout")
def logout():
session.pop("user", None)
return redirect("/login")
if __name__ == "__main__":
app.run(debug=True)
output