Login Page Flask

As a developer, I have had the opportunity to work with various web frameworks, and one that has particularly piqued my interest is Flask. Flask is a versatile and lightweight Python framework that simplifies the process of creating web applications. In this article, I will delve deeper into the process of building a login page with Flask.

Introduction to Flask

Before we get started, let’s briefly discuss what Flask is and why it’s a popular choice among developers. Flask is a microframework that follows the “Keep It Simple” principle. It provides the essentials for web development without imposing too many restrictions or dependencies. With Flask, you have the freedom to choose your database, templating engine, and other components.

Now, let’s move on to creating a login page using Flask. One of the essential features of a web application is user authentication, which allows users to securely create accounts and login to access personalized content or perform certain actions.

Setting up the Flask Application

First, we need to set up our Flask application. Assuming you have Flask installed, create a new Python file and import the necessary modules:


from flask import Flask, render_template, request, redirect, session

The Flask module is our web application instance, render_template is used to render HTML templates, request is used to handle HTTP requests, redirect is used for URL redirection, and session allows us to store and access user session data.

Next, create the Flask application instance:


app = Flask(__name__)

We also need to set a secret key for our application to securely handle session data:


app.secret_key = "your_secret_key"

Creating the Login Route

Now, let’s define the route for the login page. Add the following code to your Flask application:


@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

if username == 'my_username' and password == 'my_password':
session['logged_in'] = True
return redirect('/dashboard')
else:
return render_template('login.html', error='Invalid credentials')

return render_template('login.html')

This route handles both GET and POST requests. For GET requests, it renders the login form template, and for POST requests, it retrieves the username and password from the form and performs authentication. If the credentials are valid, the user is redirected to the dashboard page; otherwise, the login form is rendered again with an error message.

Creating the Login Form Template

To create the login form template, create a new HTML file named login.html in a templates folder. Here’s an example:


<form method="POST" action="/login">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="submit" value="Login">
</form>
{% if error %}
<p class="error">{{ error }}</p>
{% endif %}

The form sends a POST request to the /login route with the username and password fields. If there’s an error, it is displayed below the form.

Protecting Routes with Authentication

Now that we have our login page set up, we can protect certain routes by requiring authentication. For example, let’s create a dashboard page that can only be accessed by logged-in users:


@app.route('/dashboard')
def dashboard():
if 'logged_in' in session and session['logged_in']:
return "Welcome to the dashboard!"
else:
return redirect('/login')

This route checks if the user is logged in by verifying the presence of the logged_in key in the session. If the user is logged in, they are greeted with a welcome message; otherwise, they are redirected to the login page.

Conclusion

Creating a login page using Flask is relatively straightforward. By leveraging Flask’s simplicity and flexibility, we were able to build a secure authentication system for our web application. Flask provides a solid foundation for web development and offers countless possibilities for customization and extension. Whether you’re building a small personal project or a complex web application, Flask is definitely worth exploring.

Ready to get started with Flask and build your own login page? Check out the official Flask documentation for more in-depth information and examples.