Creating a web login page is a fundamental aspect of web development, and Python, with its diverse selection of web frameworks, makes this task not only possible but also incredibly straightforward. In this comprehensive guide, we'll explore how to create a simple yet functional web login page using Flask, a popular Python microframework. By the end of this tutorial, you'll have a basic understanding of user authentication in web applications using Python.

Before we dive into the code, ensure you have Python and Flask installed on your system. If not, you can install them using pip:

```python pip install flask ```
Setting Up the Project
Begin by creating a new directory for your project and navigating into it:

```bash mkdir python_web_login cd python_web_login ```
Next, create a new file called `app.py` in your project directory and import the necessary modules:
```python from flask import Flask, render_template, request, redirect, url_for, flash from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired, Email, Length ```
Creating the Flask Application

Initialize Flask and configure the template folder:
```python app = Flask(__name__) app.config['SECRET_KEY'] = 'your_secret_key' app.config['TEMPLATES_AUTO_RELOAD'] = True app.config['TEMPLATES_FOLDER'] = 'templates' ```
Create routes for the login page and the dashboard (after successful login):
```python @app.route('/') def index(): return 'Hello, World!' @app.route('/login', methods=['GET', 'POST']) def login(): # Form processing logic here... return render_template('login.html') @app.route('/dashboard') def dashboard(): #Logic to validate if user is logged in... return 'Welcome to your dashboard!' ```
Implementing the Login Form

Use Flask-WTF to create a login form. First, install the required packages if you haven't:
```python pip install flask-wtf wtforms ```
Create a new file called `forms.py` and define the login form:
```python from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, SubmitField from wtforms.validators import DataRequired, Email, Length class LoginForm(FlaskForm): email = StringField('Email', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired(), Length(min=6)]) submit = SubmitField('Login') ```
Update the `login()` route to handle form submission:

```python @app.route('/login', methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): flash('Login successful!', 'success') return redirect(url_for('dashboard')) return render_template('login.html', form=form) ```
Implementing User Authentication
For simplicity, we'll use a dictionary to store user credentials. In a real-world scenario, you'd use a database and proper hashing:




















```python users = { 'user@example.com': 'password123' } ```
Update the `dashboard()` route to check for valid credentials:
```python @app.route('/dashboard') def dashboard(): user = request.args.get('user') password = request.args.get('password') if user in users and users[user] == password: return 'Welcome to your dashboard, {}!'.format(user) else: return 'Invalid credentials', 401 ```
Creating the HTML Template
Create a new folder named `templates` and add a new file called `login.html`:
```html
Login
{{ form.password.label }}
{{ form.password(size=20) }}
{{ form.submit() }}
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %} ```
With this, you have a simple yet functional web login page using Flask. To run the application, simply add `if __name__ == '__main__':` at the end of `app.py` and execute the file:
```python if __name__ == '__main__': app.run(debug=True) ```
Now, open your browser and navigate to `http://127.0.0.1:5000/`, and you should see your login page.
While this is a simple example, always remember to use proper database management and secure your users' information in production environments. You may also want to consider using dedicated libraries for handling user authentication, such as Flask-Login or Flask-Security.