Login page in html and python

Creating a simple login page in Python can be done using various frameworks. A common choice is Flask, a lightweight web framework. Below is a basic example of how to create a login page using Flask.

Prerequisites

Make sure you have Flask installed. You can install it using pip:


Basic Flask Login Example

Here's a simple implementation:

python

from flask import Flask, render_template, request, redirect, url_for, flash, session app = Flask(__name__) app.secret_key = 'your_secret_key' # Replace with a random secret key # Dummy user data for demonstration users = { "user1": "password1", "user2": "password2" } @app.route('/') def home(): return render_template('home.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] # Check if the username exists and the password matches if username in users and users[username] == password: session['username'] = username flash('Login successful!', 'success') return redirect(url_for('dashboard')) else: flash('Invalid username or password!', 'danger') return render_template('login.html') @app.route('/dashboard') def dashboard(): if 'username' in session: return f"Welcome to your dashboard, {session['username']}!" return redirect(url_for('login')) @app.route('/logout') def logout(): session.pop('username', None) flash('You have been logged out.', 'info') return redirect(url_for('home')) if __name__ == '__main__': app.run(debug=True)

HTML Templates

You will need two HTML templates for this example. Create a folder named templates in the same directory as your Python script.

  1. home.html
html

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>Welcome to the Home Page</h1> <a href="/login">Login</a> </body> </html>
  1. login.html
html

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login Page</h1> <form method="POST"> <label for="username">Username:</label> <input type="text" name="username" required> <br> <label for="password">Password:</label> <input type="password" name="password" required> <br> <button type="submit">Login</button> </form> <br> {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} <ul> {% for category, message in messages %} <li class="{{ category }}">{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} </body> </html>

Running the Application

  1. Save the Python code in a file (e.g., app.py).
  2. Run the application:

  1. Open your browser and go to http://127.0.0.1:5000/.

Explanation

  • The app has a simple home page and a login page.
  • Users can log in with a hardcoded username and password.
  • Flash messages are used to give feedback about login attempts.
  • A session is used to keep track of the logged-in user.

This example is very basic and should not be used in production as-is. For a production application, consider implementing secure password hashing, database storage, and proper user authentication methods. If you need more specific features or security implementations, let me know.

Comments

Popular posts from this blog

What is the HTML

box office collection pushpa movie

pbks vs rcb