Creating a login page is a fundamental aspect of web development, enabling users to securely access their accounts. Python, with its simple syntax and a wealth of libraries, is an excellent choice for building web applications, including login pages. One such library is Flask, a micro web framework that is lightweight and easy to get started with. In this guide, we'll create a simple login page code example using Flask and Flask-Login, a Flask extension that handles user sessions.

Before we dive into the code, ensure you have Flask and Flask-Login installed. If not, install them using pip:

``` pip install flask flask-login ```
Setting up the Flask Application
First, let's set up a basic Flask application and import the necessary modules.

```python from flask import Flask, render_template, request, redirect, url_for from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user ```
Creating the User Class

The UserMixin class from Flask-Login provides default implementations for several methods. Let's create a simple User class.
```python class User(UserMixin): def __init__(self, id, username, password): self.id = id self.username = username self.password = password ```
Configuring Flask-Login

Now, initialize Flask and configure Flask-Login.
```python app = Flask(__name__) app.config['SECRET_KEY'] = 'your-secret-key' login_manager = LoginManager() login_manager.init_app(app) ```
User Loading and Loading Callback

Flask-Login needs a way to load a user. Here, we'll use the user id to load the user.
```python @login_manager.user_loader def load_user(user_id): # Assuming you have a list of users or a database query here users = [{'id': 1, 'username': 'user1', 'password': 'pass1'}, ...] for user in users: if user['id'] == int(user_id): return User(user['id'], user['username'], user['password']) return None ```




















Login Route
Let's create a route for the login page and handle the login logic.
```python @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] # Here you would typically query the database to check if the username and password are valid # For simplicity, we'll assume the password is correct if it matches our data users = [{'id': 1, 'username': 'user1', 'password': 'pass1'}, ...] for user in users: if user['username'] == username and user['password'] == password: user_obj = User(user['id'], user['username'], user['password']) login_user(user_obj) return redirect(url_for('dashboard')) return 'Invalid username or password' return render_template('login.html') ```
Logout Route
Flask-Login provides a built-in route decorator for handling user logout.
```python @app.route('/logout') @login_required def logout(): logout_user() return redirect(url_for('login')) ```
Protecting Routes with @login_required
Let's create a simple dashboard route that requires the user to be logged in.
```python @app.route('/dashboard') @login_required def dashboard(): return f'Welcome, {current_user.username}!' ```
To complete this example, you'll need to create a template for the login page (login.html) in a 'templates' folder. However, creating HTML templates is beyond the scope of this Python-focused guide.
This brings us to the end of our simple Flask login page example. While this example provides a basic functionality, in a real-world application, you would want to securely store user passwords (using libraries like bcrypt), handle registration, and add more features like account management and user roles. Happy coding!